//works with two words
public class Pig
{
public static void main(String args[])
{
EasyReader console = new EasyReader();
String word="";
System.out.println("Pig Latin");
System.out.print("Enter in your phrase to be converted to pig latin: ");
word=console.readLine();
String w1=pL(word);
System.out.println("Word or phrase in pig latin: "+w1);
}//end main
public static String pL(String word)
{
String w2="",w1="";
int count[] = new int[0];
int h=java.lang.reflect.Array.getLength(count);
//int h=count.length;
for(int x=0;x<word.length();x++)
{
if(word.charAt(x)==' ')
{
count=resize(count);
h=java.lang.reflect.Array.getLength(count);
count[h-1]=x;
}
}
h=java.lang.reflect.Array.getLength(count);
//h=count.length;
for(int x=0;x<=h;x++)
{
if(h==0)
{
w2=word;
w1=pig(w2);
break;
}
else
{
if(x==0)
{
w2=word.substring(0,count[0]);
w2=pig(w2);
//w1+=w2;
w1+=" "+w2;
}
else
{
if(x==h)
{
w2=word.substring(word.lastIndexOf(' ')+1);
w2=pig(w2);
w1+=" "+w2;
}
else
{
//if(x)
w2=word.substring(count[x],word.lastIndexOf(' ')+1);
w2=pig(w2);
//w1+=w2;
w1+=" "+w2;
}
}
}
}
return w1;
}
public static int[] resize(int[] count)
{
int newArray[]=new int[count.length+1];
for(int x=0;x<count.length;x++)
{
newArray[x]=count[x];
}
return newArray;
}
public static String pig(String w2)
{
String reWord="";
char i=w2.charAt(1),d=w2.charAt(0);
if(d=='a'||d=='e'||d=='i'||d=='o'||d=='u'||d=='A'||d=='E'||d=='I'||d=='O'||d=='U')
reWord=w2+"way";
else if((d!='a'||d!='e'||d!='i'||d!='o'||d!='u'||d!='A'||d!='E'||d!='I'||d!='O'||d!='U')&&
(i=='a'||i=='e'||i=='i'||i=='o'||i=='u'||i=='A'||i=='E'||i=='I'||i=='O'||i=='U'))
reWord=w2.substring(1)+d+"ay";
else if((d!='a'||d!='e'||d!='i'||d!='o'||d!='u'||d!='A'||d!='E'||d!='I'||d!='O'||d!='U')&&
(i!='a'||i!='e'||i!='i'||i!='o'||i!='u'||i!='A'||i!='E'||i!='I'||i!='O'||i!='U'))
reWord=w2.substring(2)+d+i+"ay";
return reWord;
}
}
With the errors:
Quote
Enter in your phrase to be converted to pig latin: happy child awesome
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at Pig.pig(Pig.java:97)
at Pig.pL(Pig.java:63)
at Pig.main(Pig.java:11)
Thank you in advance for any suggestions

New Topic/Question
Reply



MultiQuote










|