Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Getting a string index out of bounds error when trying to run my string functions
    primarykey
    data
    text
    <pre><code>package unl.cse.strings; import java.lang.Character; import java.lang.String; public class StringUtils { public static int longestCommonPrefix(String a, String b){ int sizeOfResult=0; int size=0; int i=0; if (a.length() == 0 || b.length() == 0 ){ return sizeOfResult; } else if (a.length()&gt;b.length()){ size = a.length(); } else { size = b.length(); } for(i=0; i&lt;size; i++){ if(a.charAt(i) != b.charAt(i)){ break; } sizeOfResult++; } return sizeOfResult; } public static String removeBlanks(String s){ StringBuilder sb = new StringBuilder(); for(int i=0; i&lt;s.length(); i++) { if(!Character.isWhitespace(s.charAt(i))) { sb.append(s.charAt(i)); } } return sb.toString(); } public static String flipCase(String s){ StringBuilder sb = new StringBuilder(); int i = 0; for (i=0;i&lt;s.length();i++) { if (Character.isLetter(s.charAt(i))) { if (Character.isUpperCase(s.charAt(i))){ sb.append(Character.toLowerCase(s.charAt(i))); } if (Character.isLowerCase(s.charAt(i))) { sb.append(Character.toUpperCase(s.charAt(i))); } } else { sb.append(Character.toUpperCase(s.charAt(i))); } } return sb.toString(); } } </code></pre> <p>When I compile the program I'm getting:</p> <blockquote> <p>Testing longestCommonPrefix(glossary, gloss) ... Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:658) at unl.cse.strings.StringUtils.longestCommonPrefix(StringUtils.java:17) at unl.cse.strings.StringUtilsGrader.testLCP(StringUtilsGrader.java:15) at unl.cse.strings.StringUtilsGrader.main(StringUtilsGrader.java:78) WARNING: process exited with a(n) Unknown (1) error code</p> </blockquote> <p>I'm not sure where I'm trying to access a part of the string that's out of the range.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload