Results 1 to 13 of 13
  1. #1
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default Out of bounds exception Errors (Trouble finding problem)

    I have a program that takes a word(Tuesday) and is supposed to translate into pig latin. The rule of pig latin are:

    1) Take all the consonants before the first vowel you see (i.e. Tuesday = uesdayT)

    2) Then add ay to the end.

    3) If there are no vowels in the word or it begins with a vowel, just add ay to the end. (i.e. Cry = Cryay or at = atay)


    I'll post all of my code but not all of it is relevant to my question. I will specify which part is: (locate the part that says else if (translator == 4){)

    I have a String array named [words]. It contains the word (Tuesday).


    I will have comments so that you can follow what I hope my code is doing.
    Note: I am doing this for one word entries only



    I also have a character array named [vowels]. It contains (a, e, i, o, u).

    I have a character array named [temp]. It is an empty temporary memory space to hold each consonant that needs to be moved to the rear of each word.

    I get an out of bounds error on line 121, but don't know how to fix it

    My code is first followed by my MAIN


    Java Code:
    package package10;
    
    
    public class Xlator {
    
    
    	public static final int UNCHANGED = 0;
    
    	public static final int LOWERCASE = 1;
    
    	public static final int UPPERCASE = 2;
    
    	public static final int ALTERNATE = 3;
    
    	public static final int PIGLATIN = 4;
    
    
    
    
    	public static String getAuthor(){
    
    
    		return "Sid Patel";
    
    	}
    
    
    	public static String Xlate(int translator, String sentence){
    
    		if (translator == 1){
    
    
    			return sentence.toLowerCase();
    
    		}
    
    
    		else if (translator == 2){
    
    
    			return sentence.toUpperCase();
    
    		}
    
    
    		else if (translator == 3){
    
    
    
    
    			for (int i = 0 ; i < sentence.length () ; i++)
    
    			{
    
    				if (i % 2 == 0)
    
    				{
    
    					sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
    
    				}
    
    				if (i % 2 != 0)
    
    				{
    
    					sentence = sentence.substring (0, i) + Character.toLowerCase(sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
    
    				}
    
    			}
    
    			return sentence;
    
    		}
    
    
    		else if (translator == 4){
    
    			
    			String [] words = sentence.split("[ .,?!]+");
    			// Splits the sentence into words 
    			
    			char [] vowels = {'a','e','i','o','u'}; 
    			// My vowels 
    			
    			for(int position = 0; position <= words.length; position++){
    				// Loop
    				//Position stands for the position of the words array.
    				// 0 stands for position 0 in the words array
    				
    
    
    				char [] letters = words[position].toCharArray();
    				// Changes the word, based on which position is selected into a character array
    				// If position one, the first word in the array will be changed into letters
    				// If position two, the second word in the array will be changed into letters
    				
    
    				for(int i = 0; i <= vowels.length; i++){
    					// Loop
    					// variable i is the position in the vowels array
    					
    					if (letters[0] == vowels[0]){
    						words[position] = words[position] + "ay";
    					}
    					// If statement states: if the first letter in letters array is "a", then just "ay"
    					// I know that i have to do this for the other vowels as well but I am taking it slow
    					// This is why I have chosen the word "tuesday"
    					
    					else{
    						char temp = letters[0];
    						// Create a temporary array that will hold the first letter in the letters array(Word is Tuesday)
    						// It will hold "T"
    						
    						for(int j = 0; j <= letters.length; j++){
    							// Loop
    							// If the first letter in the letters array is not a vowel do this
    							
    							
    							letters[j] = letters[j + 1];
    							// The first letter from tuesday has been copied to the character array temp
    							// This takes position  0 in character array letters and replaces it with position 1 and so 
    							
    						letters[letters.length - 1] = temp;
    						//  Tuesday has 7 letters which is 7 in length. 
    						// Letters has 6 positions counting 0.
    						// This takes the length 7 and subtracts by 1 = position 6 in the array or the last position
    						// then sets the last position in chracter array letters to chracter array temp
    						// Position 6 now = T
    						
    						sentence = letters[j]+ "ay";
    						// We only have one word "tuesday", which is position 0 in string array words
    						
    						
    					}
    				}
    			}
    
    			}
    		}
    
    		return sentence;
    
    	}
    
    
    
    
    }




    Java Code:
    package package10;
    
    import javax.swing.JOptionPane;
    
    public class XlatorMain {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		// if you don't have one (or more) of these translators implemented, jut comment out the line
    
    		int [] translator = { 
    				Xlator.UNCHANGED,
    				Xlator.LOWERCASE,
    				Xlator.UPPERCASE,
    				Xlator.ALTERNATE,
    				Xlator.PIGLATIN,
    				//Xlator.SWEDISHCHEF
    		};
    		
    		String input = JOptionPane.showInputDialog(null,"Enter the text for translation: ");
    		
    		while (input!= null) {
    					
    			String outputString = "";
    			
    			for (int j = 0; j < translator.length; j++) {
    				String ans1 = Xlator.Xlate(translator[j], input);
    				outputString = outputString + "\n'" + input + "' translates to '" + ans1 + "' using method " + translator[j];
    			}
    			
    			input = JOptionPane.showInputDialog(null,outputString + "\n\nEnter the text for translation: ");
    		}
    	}
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I honestly haven't looked at all your code, but I did do a quick search of the for loops, and this bugs me:

    Java Code:
    			for (int i = 0 ; i < sentence.length () ; i++)
    			{
    				if (i % 2 == 0)
    				{
    					sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
    				}
    				if (i % 2 != 0)
    				{
    					sentence = sentence.substring (0, i) + Character.toLowerCase(sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
    				}
    			}
    			return sentence;
    Because you're using sentence length to determine how long to loop, and you're changing the String held by sentence from within the loop, which smells like a recipe for disaster. What happens when you shorten sentence but your loop keeps on looping as if sentence were its original length? Perhaps you don't want to change sentence from within the loop but instead use a dummy variable and then set sentence = to the dummy variable after the loop, or perhaps you want to change sentence but then break out of the loop? I'm not sure because again (sorry), but I didn't read all the code.

    HTH

  3. #3
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    You would be correct, but I'm not changing the length. The loop is in place to keep track of which letter in the sentence to change, never to remove it which changes the length.

    Essentially, all I am doing is taking a word and changing the cases of every letter like this. HeLlO.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by sidd0123 View Post
    You would be correct, but I'm not changing the length. The loop is in place to keep track of which letter in the sentence to change, never to remove it which changes the length. Essentially, all I am doing is taking a word and changing the cases of every letter like this. HeLlO.
    But which line throws the exception? Is it coming from this for loop?

  5. #5
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    letters[j] = letters[j + 1];

    is what gives me an error.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by sidd0123 View Post
    letters[j] = letters[j + 1];

    is what gives me an error.
    Arrays are 0 based. So the letters array goes from 0 to letters.length - 1.

  7. #7
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    Still get an out of bounds error, this time by -1

    Since j starts out at 0, it says 0-1 ohh a negative number, error
    and when adding

    since j starts out at 0, it says 0 + 1, it works, but when it reaches the end of the array, position 6 and adds 1 it says over extended array boundaries.

    plug my code and main into Eclipse and see what happens. I have no idea how to fix this

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    A few style points, just because...

    Unless you are specifically instructed not to do so, you should break up your code into more methods. This will make it easier to read and understand.

    You have defined several constants at the beginning of your code. Use them.

    Putting those two tips together, you would get this:
    Java Code:
    	public static String Xlate(int translator, String sentence){
    		if (translator == LOWERCASE) {
    			return sentence.toLowerCase();
    		}
    		else if (translator == UPPERCASE) {
    			return sentence.toUpperCase();
    		}
    		else if (translator == ALTERNATE) {
    			return alternateCase(sentence);
    		}
    		else if (translator == PIGLATIN) {
    			return pigLatin(sentence);
    		}
    		// translator == UNCHANGED
    		return sentence;
    	}
    I think you'll agree that this is much easier to read and understand.

    Your alternate code is splitting up your sentence String and putting it back together for each character in the String. This is inefficient (not worth worrying about) and complicated for reading and understanding (worth fixing). Consider this alternative:
    Java Code:
    		String result = "";
    		for (int i = 0; i < sentence.length(); i++) {
    			if (i % 2 == 0) {
    				result += Character.toUpperCase(sentence.charAt(i));
    			} else {
    				result += Character.toLowerCase(sentence.charAt(i));
    			}
    		}
    		return result;
    ...or even better...
    Java Code:
    		int counter = 0;
    		StringBuilder sb = new StringBuilder(sentence.length());
    		for (char c : sentence.toCharArray()) {
    			if (counter++ % 2 == 0) {
    				sb.append(Character.toUpperCase(c));
    			} else {
    				sb.append(Character.toLowerCase(c));
    			}
    		}
    		return sb.toString();
    Consider moving your vowels array and making it a private static final (constant) in your class.

    Consider breaking up pigLatin(String) and writing a pigLatinWord(String) as well.

    -Gary-

  9. #9
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    Sorry for the really late reply, but I still have a problem with the out of bounds exception, any clues as to what the problem could be?

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Show us your code as it is now, and indicate which line is giving you the ArrayIndexOutOfBoundsException.

    -Gary-

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Not that this has anything to do with the problem, but arguably "y" is a vowel, so Cry would be ycray.

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    Not that this has anything to do with the problem, but arguably "y" is a vowel, so Cry would be ycray.
    This is true, but assignments like this generally overlook that detail. It's a little difficult to determine when y is a vowel. A decent heuristic would be to assume that it is a consonant when followed by a, e, i, o or u, and a vowel otherwise. But that's not a perfect rule (bye, dye, dryad).

    -Gary-

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    This is true, but assignments like this generally overlook that detail. It's a little difficult to determine when y is a vowel. A decent heuristic would be to assume that it is a consonant when followed by a, e, i, o or u, and a vowel otherwise. But that's not a perfect rule (bye, dye, dryad).
    AFAIK it's a diphtong as in 'why' or 'aye' (we have similar 'problems' in Dutch).

    kind regards,

    Jos

Similar Threads

  1. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  2. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 PM
  3. Pleas help on finding errors
    By McXxT in forum New To Java
    Replies: 6
    Last Post: 04-02-2009, 08:42 AM
  4. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 07:21 PM
  5. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •