Results 1 to 13 of 13
- 05-01-2010, 09:34 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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: "); } } }
- 05-01-2010, 09:48 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
I honestly haven't looked at all your code, but I did do a quick search of the for loops, and this bugs me:
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.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;
HTH
- 05-01-2010, 09:54 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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.
- 05-01-2010, 10:09 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-01-2010, 10:23 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
letters[j] = letters[j + 1];
is what gives me an error.
- 05-01-2010, 10:29 PM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-01-2010, 11:00 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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
- 05-01-2010, 11:41 PM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
I think you'll agree that this is much easier to read and understand.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; }
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:
...or even better...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;
Consider moving your vowels array and making it a private static final (constant) in your class.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 breaking up pigLatin(String) and writing a pigLatinWord(String) as well.
-Gary-
- 05-05-2010, 09:23 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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?
- 05-05-2010, 09:58 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Show us your code as it is now, and indicate which line is giving you the ArrayIndexOutOfBoundsException.
-Gary-
- 05-06-2010, 10:04 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Not that this has anything to do with the problem, but arguably "y" is a vowel, so Cry would be ycray.
- 05-06-2010, 10:11 AM #12
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 05-06-2010, 10:54 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM -
Array out of bounds exception 20.
By dropt in forum New To JavaReplies: 4Last Post: 09-21-2009, 10:32 PM -
Pleas help on finding errors
By McXxT in forum New To JavaReplies: 6Last Post: 04-02-2009, 08:42 AM -
Java ArrayList out of bounds exception
By grahamb314 in forum New To JavaReplies: 5Last Post: 11-22-2008, 07:21 PM -
[SOLVED] out of bounds exception help
By soxfan714 in forum New To JavaReplies: 21Last Post: 11-11-2008, 08:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks