-2

I have a project that instructs to create a java class with 2 methods: one that translates a word to pig latin, another that translates a whole sentence to pig latin. I am supposed to go about this by extracting each word from a sentence, put it through my word translate method, and return the finalized sentence with each translated word.

I cannot use function such as String.split, and other shortcut functions.

My code looks like this:

public String translateWord (String word ) {
    
    if (word.charAt(0) == 'q'  && word.charAt(1) == 'u') {
        return word.substring(2) + "quay";
    }
     
    else if ( word.charAt(0) == 'a' || word.charAt(0) == 'e' || word.charAt(0) == 'i' || word.charAt(0) == 'o' || word.charAt(0) == 'u') {
         return word + "way";
     }
     
     else 
         return word.substring(1) + word.charAt(0) + "ay";
 }

 
public  String translateSentence ( String sentence ) {
    String finalStr, temp;
    int index = sentence.indexOf(" ");       
    temp = sentence.substring ( 0, index );
    finalStr = translateWord(temp); 
    for ( int i=index+1; i < sentence.length()-2; i++ ) {
         if ( sentence.substring(i, i+1).equals(" ")) {
             temp = sentence.substring(i+1, sentence.indexOf( " ", i+1) )  ;
             finalStr = finalStr + translateWord(temp); 
         }
    }
     return finalStr;
}
        
 }
 

These are the messages appearing:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 10, end -1, length 18
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3756)
at java.base/java.lang.String.substring(String.java:1902)
at PigLatin.translateSentence(PigLatin.java:25)
at driver.main(driver.java:6) 

There are no problems with the word method, it's just there for reference. However, can anyone offer any explanation for why the sentence method does not work and help on how to fix it?

Thanks in advance, let me know if any further explanation is needed.

jacob
  • 1
  • 1
  • `begin 10, end -1, length 18` - you attempted to extract a substring starting at position 10 ending at position -1. Step through your code in a debugger to see where this happening and why. – Jim Garrison Feb 07 '21 at 01:48
  • If you **read the documentation**, i.e. the javadoc of [`indexOf()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#i23), you'll see: *"Returns the index of the first occurrence of the specified substring, or **-1 if there is no such occurrence**."* --- Since you use `indexOf()` in 2 places, and in both places you never check for -1, and you give the value as 2nd argument to `substring()`, and since the error says `end -1`, it's very obvious that `indexOf()` didn't find anything, and that's why it failed. – Andreas Feb 07 '21 at 02:04
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Feb 07 '21 at 02:05
  • *FYI:* `sentence.substring(i, i+1).equals(" ")` is better written as `sentence.charAt(i) == ' '` – Andreas Feb 07 '21 at 02:06

1 Answers1

0

The problem appears to be at this line.

temp = sentence.substring(i+1, sentence.indexOf( " ", i+1) ) ;

The documentation of indexOf function says the following

Returns: An int value, representing the index of the first occurrence of the character in the string, or -1 if it never occurs

At some point in the loop your code does not find anymore spaces and then the indexOf returns - 1 which is an invalid end index for the substring function. Hence the exception.

vvs
  • 1,066
  • 8
  • 16
  • I thought this is where the problem was. Im still lost on how to fix it, do you have any suggestions? – jacob Feb 07 '21 at 02:02
  • Yes. split that code into two lines. Run indexOf first. Run the substring only if index is greater than i+1 – vvs Feb 07 '21 at 02:04