Pig Latin help

I am having a problem with phrases

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2446 Views - Last Post: 18 March 2010 - 10:27 AM Rate Topic: -----

#1 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Pig Latin help

Posted 13 March 2010 - 09:51 PM

I have an assignment for a Pig Latin Translator. Typical project with the rules the same as all of the others. My problem is that with more than three words, I receive an Out of Bounds error, and I do not know how to fix it. Here's my code:

//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

Pig Latin
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

Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin help

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin help

Posted 13 March 2010 - 10:46 PM

Here:
public static String pL(String word)                                                    
        {
                
                String w2="",w1="";
                int count[] = new int[0];


In the first bit of this method, you declare a String w2 which you later call here:
ublic 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=


w2 only has scope for the pL(String ) method. If you want to access it again later in other methods, you need to declare it outside of the method.

Also here:
public static int[] resize(int[] count)
        {
                int newArray[]=new int[count.length+1];
                for(int x=0;x<count.length;x++)
                {


You declare a new array that is the length of count's length + 1. But then you call the count.length in the for loop. count.length still equals 1, you can't increment count and get anywhere. You should be calling newArray.length in the for loop.

This post has been edited by zim1985: 14 March 2010 - 12:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Pig Latin help

Posted 13 March 2010 - 11:31 PM

Quote

w2 only has scope for the pL(String ) method. If you want to access it again later in other methods, you need to declare it outside of the method.


w2 is actually a parameter for the method :)
public static String pig(String w2)




Also, OP, you may want to look into ArrayLists, which works almost like an array, but can have elements added and removed from it at any time

This post has been edited by erik.price: 13 March 2010 - 11:35 PM

Was This Post Helpful? 1
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin help

Posted 13 March 2010 - 11:35 PM

View Posterik.price, on 13 March 2010 - 09:31 PM, said:

Quote

w2 only has scope for the pL(String ) method. If you want to access it again later in other methods, you need to declare it outside of the method.


w2 is actually a parameter for the method :)
public static String pig(String w2)


Regardless, the effect is the same. You can't call something that has method scope in another method later on.
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Pig Latin help

Posted 13 March 2010 - 11:44 PM

Maybe you're right, but I'm not quite understanding what you mean (it is 1:30 after all ;)).

The pig method takes a String, w2, works with it, and then returns a modified String.

In the pL method, the pig method is called like so:
w2=pig(w2);



Tell me if I'm misunderstanding.
Was This Post Helpful? 1
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin help

Posted 14 March 2010 - 12:06 AM

Hrumph. I just looked at it again, seems like these are 2 different w2 objects. That's kinda confusing - a bad programming habit if you ask me.
Was This Post Helpful? 0
  • +
  • -

#7 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 14 March 2010 - 10:23 AM

what would be the fix to that problem then?


Thanks for the advice by the way.


with the

Quote

Also here:
public static int[] resize(int[] count)
        {
                int newArray[]=new int[count.length+1];
                for(int x=0;x<count.length;x++)
                {



You declare a new array that is the length of count's length + 1. But then you call the count.length in the for loop. count.length still equals 1, you can't increment count and get anywhere. You should be calling newArray.length in the for loop.


i changed the count.length to newArray.length and then i ran the program and got

Quote

Pig Latin
Enter in your phrase to be converted to pig latin: happy child awesome
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Pig.resize(Pig.java:90)
at Pig.pL(Pig.java:26)
at Pig.main(Pig.java:12)

for an error..

with what you said before i declared w2 out of main as private static String w2="";

This post has been edited by Parrish: 14 March 2010 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#8 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 15 March 2010 - 09:59 AM

I don't understand why the out of bounds went for 1 to 0, any suggestions?
Was This Post Helpful? 0
  • +
  • -

#9 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 15 March 2010 - 10:09 AM

View Postzim1985, on 13 March 2010 - 09:46 PM, said:

Here:
public static String pL(String word)                                                    
        {
                
                String w2="",w1="";
                int count[] = new int[0];


In the first bit of this method, you declare a String w2 which you later call here:
ublic 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=


w2 only has scope for the pL(String ) method. If you want to access it again later in other methods, you need to declare it outside of the method.

Also here:
public static int[] resize(int[] count)
        {
                int newArray[]=new int[count.length+1];
                for(int x=0;x<count.length;x++)
                {


You declare a new array that is the length of count's length + 1. But then you call the count.length in the for loop. count.length still equals 1, you can't increment count and get anywhere. You should be calling newArray.length in the for loop.


Sorry for the DP, but i went over this again with what you suggested, and where you wanted me to switch count.length() to newArray.length(); I did so, but when I entered in two words it gave me an error

Quote

Pig Latin
Enter in your phrase to be converted to pig latin: happy child
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at PigLatin_Parrish.resize(PigLatin_Parrish.java:91)
at PigLatin_Parrish.pL(PigLatin_Parrish.java:27)
at PigLatin_Parrish.main(PigLatin_Parrish.java:12)

Process completed.


and as I entered in one word, it workes just fine.

How should I approach this?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Pig Latin help

Posted 15 March 2010 - 03:09 PM

Why don't you use an ArrayList, as erik.price suggested? It will dynamically resize, so you don't have to mess with resizing it. Also, can you post your revised code and the input?

For more information on ArrayList, check out the API:
http://java.sun.com/.../ArrayList.html
Was This Post Helpful? 0
  • +
  • -

#11 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 15 March 2010 - 07:11 PM

i looked at that earlier today, and i wasn't sure really how to use it
Was This Post Helpful? 0
  • +
  • -

#12 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 15 March 2010 - 08:31 PM

Alright, I haven't implemented the ArrayList, because I am not exactly sure how to use it, but here is my code

//works with two words
public class Pig
{
	private static String w2="";
	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 w1="",w3="";
		int count[] = new int[0];
		int s=0;//space counter?
		int h=java.lang.reflect.Array.getLength(count);
		//int h=count.length;
		for(int x=0;x<word.length();x++)
		{
			if(word.charAt(x)==' ')
			{
				s++;
				count=resize(count,s);
				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(word);
				break;
			}	
			else 
			{
				if(x==0)
				{
					w2=word.substring(0,count[0]);
					w3=pig(w2);
					//w1+=w2;
					w1+=" "+w3;
				}
				else
				{
					if(x==h)
					{
						w2=word.substring(word.lastIndexOf(' ')+1);
						w3=pig(w2);
						w1+=" "+w3;
					}
						
					else
					{
						//if(x)
						w2=word.substring(count[x],word.lastIndexOf(' ')+1);
						w3=pig(w2);
						//w1+=w2;
						w1+=" "+w3;
					}	
				}
			}	
		}
		return w1;	
	}
/*	public static Object resize(int[] count)//found on internet, editted to personal use
	{
		int h=java.lang.reflect.Array.getLength(count);
		//int h=count.length;
		Class elementType = count.getClass().getComponentType();
		Object newArray = java.lang.reflect.Array.newInstance(elementType,h+1);
		int preserveLength = Math.min(h,h+1);
		if (preserveLength > 0)
			System.arraycopy (count,0,newArray,0,preserveLength);
		return newArray;
	}*/
	public static int[] resize(int[] count, int s)
	{
		
		int newArray[]=new int[s+1];
		//for(int x=0;x<newArray.length;x++)
		//for(int x=0;x<s;x++)
		//{
			System.arraycopy(count,0,newArray,0,s);
			//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;
	}
}


now this works with one =( I fail...
And here is one input

Quote

Pig Latin
Enter in your phrase to be converted to pig latin: happy
Word or phrase in pig latin: appyhay


Input with 2 words:

Quote

Pig Latin
Enter in your phrase to be converted to pig latin: happy child
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at Pig.resize(Pig.java:92)
at Pig.pL(Pig.java:28)
at Pig.main(Pig.java:12)


with this i understand that the new method i put in there didnt really work..
i then changed the System.arrayCopy to
public static int[] resize(int[] count, int s)
	{
		
		int newArray[]=new int[s+1];
		for(int x=0;x<newArray.length;x++)
		//for(int x=0;x<s;x++)
		{
			//System.arraycopy(count,0,newArray,0,s);
			newArray[x]=count[x];
		}
		return newArray;
	}


and got the output of two words like this:

Quote

Pig Latin
Enter in your phrase to be converted to pig latin: happy child
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Pig.resize(Pig.java:93)
at Pig.pL(Pig.java:28)
at Pig.main(Pig.java:12)


I am not quite sure on how to put in the arrayList methods in here

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#13 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin help

Posted 15 March 2010 - 09:14 PM

You declare an ArrayList like this:
ArrayList<Object Type> list = new ArrayList<Object Type>();
//-----------------------------------------------

ArrayList<Integer> list = new ArrayList<Integer>();
// to add things to it
list.add(1);
list.add(2);
// to get things from it
int num1 = list.get(0);
int num2 = list.get(1);



When declaring an ArrayList, you have to, in the sideways carrots, declare the Object Class that will be in the List. If you want to use a primitive, you have to use their respective wrapper classes.
Integer = int
Double = double
Character = char
Float = float
Short = short
Byte = byte
Long = long

Then instead of using
int[] list = new int[10];
list[0] = 1;
int num = list[0];



You would use
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
int num = list.get(0);



You can easily iterate through an ArrayList just as you do a regular array! So this should be cinch to transfer over to your program.

There are a whole bunch of useful methods in the ArrayList class. Check it out: http://java.sun.com/.../ArrayList.html

This post has been edited by zim1985: 15 March 2010 - 09:16 PM

Was This Post Helpful? 1
  • +
  • -

#14 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Pig Latin help

Posted 16 March 2010 - 10:11 AM

View Postzim1985, on 15 March 2010 - 08:14 PM, said:

You declare an ArrayList like this:
ArrayList<Object Type> list = new ArrayList<Object Type>();
//-----------------------------------------------

ArrayList<Integer> list = new ArrayList<Integer>();
// to add things to it
list.add(1);
list.add(2);
// to get things from it
int num1 = list.get(0);
int num2 = list.get(1);



When declaring an ArrayList, you have to, in the sideways carrots, declare the Object Class that will be in the List. If you want to use a primitive, you have to use their respective wrapper classes.
Integer = int
Double = double
Character = char
Float = float
Short = short
Byte = byte
Long = long

Then instead of using
int[] list = new int[10];
list[0] = 1;
int num = list[0];



You would use
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
int num = list.get(0);



You can easily iterate through an ArrayList just as you do a regular array! So this should be cinch to transfer over to your program.

There are a whole bunch of useful methods in the ArrayList class. Check it out: http://java.sun.com/.../ArrayList.html


by trying add in the ArrayList, it gives me a cannot find symbol error with the ArrayList class, is there something I am missing, like adding a library or not using something
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Pig Latin help

Posted 16 March 2010 - 10:14 AM

Yes...you need to import java.util.ArrayList.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2