-3
import java.util.Scanner;
import java.util.Arrays;

public class PigLatin
{



  public static void main (String[] args) {
   System.out.println("Translate or Reverse");
   Scanner choose = new Scanner(System.in); 
   String choice = choose.nextLine();
   Scanner input = new Scanner(System.in);
   String ipWord = input.nextLine();




    if ( choice.equals("Translate"))
    {


      System.out.println(translator(ipWord));

    }
    else if( choice.equals("Reverse"))
    {
      System.out.println(reverse(ipWord));
    }
    else 
    {
      System.out.println("Error");
    }

}
  public static String translator(String ipWord)

  {
    String[] wordsArray = ipWord.split(" ");
    String result = "";

    for(int j =0;j<wordsArray.length;j++)
    {

      if(wordsArray[j].charAt(0)=='a' || wordsArray[j].charAt(0) =='e' || wordsArray[j].charAt(0)== 'i' || wordsArray[j].charAt(0)== 'o' || wordsArray[j].charAt(0)== 'u'|| wordsArray[j].charAt(0)=='A' || wordsArray[j].charAt(0) =='E' || wordsArray[j].charAt(0)== 'I' || wordsArray[j].charAt(0)== 'O' || wordsArray[j].charAt(0)== 'U')
/* I check if it is begin with vowel. If it begin with vowels + "yay". For example "Eat">"Eatyay"*/
      {
        result = result+ " " + wordsArray[j] +"yay";
      }

      else 
      {
        for ( int i = 1; i<= wordsArray.length; i++)
        {
          if(wordsArray[j].charAt(0)=='B'||wordsArray[j].charAt(0)=='C'||wordsArray[j].charAt(0)=='D'||wordsArray[j].charAt(0)=='F'||wordsArray[j].charAt(0)=='G'||wordsArray[j].charAt(0)=='H'||wordsArray[j].charAt(0)=='J'||wordsArray[j].charAt(0)=='K'||wordsArray[j].charAt(0)=='L'||wordsArray[j].charAt(0)=='M'||wordsArray[j].charAt(0)=='N'||wordsArray[j].charAt(0)=='P'||wordsArray[j].charAt(0)=='Q'||wordsArray[j].charAt(0)=='R'||wordsArray[j].charAt(0)=='S'||wordsArray[j].charAt(0)=='T'||wordsArray[j].charAt(0)=='V'||wordsArray[j].charAt(0)=='X'||wordsArray[j].charAt(0)=='Z'||wordsArray[j].charAt(0)=='W'||wordsArray[j].charAt(0)=='Y')
/* I check if it begin with a UpperCase Consonants, then I will take every consonant before the first vowel, move it to the back and add "ay"
 . For example, "World" > "Orldway" ; "String" > "Ingstryay" */
          {
            if (wordsArray[j].charAt(i)=='a' || wordsArray[j].charAt(i) =='e' || wordsArray[j].charAt(i)== 'i' || wordsArray[j].charAt(i)== 'o' || wordsArray[j].charAt(i)== 'u')
            {
              result = result+ " " + wordsArray[j].substring(i,i+1).toUpperCase( )+ wordsArray[j].substring(i+1)+wordsArray[j].substring(i-1,i).toLowerCase( ) +"ay";
            }


          }

          else 
          {
            if (wordsArray[j].charAt(i)=='a' || wordsArray[j].charAt(i) =='e' || wordsArray[j].charAt(i)== 'i' || wordsArray[j].charAt(i)== 'o' || wordsArray[j].charAt(i)== 'u'|| wordsArray[j].charAt(j)=='A' || wordsArray[j].charAt(j) =='E' || wordsArray[j].charAt(j)== 'I' || wordsArray[j].charAt(j)== 'O' || wordsArray[j].charAt(j)== 'U')
            {
              result = result+ " " + wordsArray[j].substring(i)+ wordsArray[j].substring(0,i) +"ay";
            }

          }



        }


      }

    }
    return result;
  }
  public static String reverse ( String ipWord)
  {
    String[] wordsArray = ipWord.split(" ");
    String result = "";
    for( int h = 0 ; h < wordsArray.length ; h++)
    {
      if(wordsArray[h].charAt(wordsArray[h].length( )-3)=='y')

      {
        result = result + " "  + wordsArray[h].charAt(wordsArray[h].length()-4) + wordsArray[h].substring(0,wordsArray[h].length()-4);

      }
      else
      {
        result = result + wordsArray[h].substring(0,wordsArray[h].length( )-2);
      }
    }
    return result;
  }
} 

Here what I fix. I think it can't not translate more than 2 more words. If I try to translate Hi how are you. The error is :

java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.charAt(Unknown Source)
    at PigLatin.translator(PigLatin.java:56)
    at PigLatin.main(PigLatin.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)*/
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

Always trust the errors. They know what they're saying. If they say you're out of range - you are indeed out of range, and you even know exactly where. You didn't mark the line but it wasn't hard to spot:

if (wordsArray[j].charAt(i)=='a' || ...

What could be out of range? Either wordsArray[j] doesn't exist or charAt(i) doesn't. Which is it?

j is bound by the amount of words in wordsArray. We're in the clear here. That leaves the other one. Now, what does i mean...

for ( int i = 1; i<= wordsArray.length; i++)

It would seem that it also is bound by the amount of words in wordsArray. Starting with 1 so it will never show the first word, but that's irrelevant since getting words is not what you use it for. In effect what causes the error is that you want your j-th word to have as much letters as you have words in the input.

Deltharis
  • 2,320
  • 1
  • 18
  • 29