Results 1 to 4 of 4
- 09-17-2012, 04:25 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
java.lang.ArrayIndexOutOfBoundsException
I am trying to create a program that reads the user's input, and capitalizes the first letter of every first word. I am apparently having array problems when I convert my string to a char array. I'm sure I am probably using Character.isLetterOrDigit() incorrectly, but it doesn't give me any errors. Only the array is.
Here is what my console window looks like after I input the string and press enter:
Here is my code, I have commented out the lines that cause the program to fail:Please type a message:
hello world
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 104
at UpperAndLower.main(UpperAndLower.java:39)
Edit: After running it another time, apparently my .toLowerCase() function is also not working. Now I'm super boggled.Java Code:import java.util.Scanner; public class UpperAndLower { public static void main(String[] args) { System.out.println("Please type a message: \n"); Scanner a = new Scanner(System.in) ; String Input ; while( a.hasNext()) { Input = a.nextLine(); Input.toLowerCase(); int Length = Input.length(); char[] CurrentChar = new char [Length]; for (int count=0; count < Length; count++) { CurrentChar = Input.toCharArray(); if (count == 0 && Character.isLetterOrDigit(Input.charAt(count)) == true) { // Character.toUpperCase(CurrentChar[Input.charAt(count)]); System.out.printf("%c", CurrentChar[count]); } // if count = 0 else if (Character.isLetterOrDigit(Input.charAt(count)) == false) { // Character.toUpperCase(CurrentChar[Input.charAt(count)]); System.out.printf("%c", CurrentChar[count]); } // else if false else System.out.printf("%c", CurrentChar[count]); } // for count System.out.println("\n"); } // while hasNext a.close() ; } // void main } // class UpperAndLower
-
Re: java.lang.ArrayIndexOutOfBoundsException
toLowerCase() works just fine. The key concept here is that String objects are constants and (short of fancy reflection) cannot be changed. In other words, they are immutable. So calling toUpperCase() or toLowerCase() does nothing to the String that the method is being called on, but instead these methods return Strings that are alterations of the original String.
So it's not
but rather:Java Code:Input = a.nextLine(); Input.toLowerCase();
Also, you'll want to change your code so that it obeys Java naming rules, else it will be hard for us and others to read and understand.Java Code:Input = a.nextLine(); Input = Input.toLowerCase(); // note the difference!
- 09-17-2012, 04:32 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: java.lang.ArrayIndexOutOfBoundsException
Gah, of course. It was working earlier, not sure why I removed that. Thanks for the catch. Maybe I was just tired.
.gif)
I'm still having trouble with the two lines that are commented out. When I remove the comment on the second line, here is my output. It is outputting everything until it reaches the white space.
Sorry about that, I'm still pretty new to this. I've copied some of the code right out of my book, and the most of it I've just poked around with until I made it work. Which variables in particular?Please type a message:
Hello World
hello Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
at UpperAndLower.main(UpperAndLower.java:45)
And thank you for your reply!
- 09-18-2012, 08:38 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: java.lang.ArrayIndexOutOfBoundsException
Fixed the run time error,
instead ofJava Code:CurrentChar[count] = Character.toUpperCase(CurrentChar[count]);
Also fixed a few other array boundary bugs by using a count ++ and a length -1Java Code:Character.toUpperCase(CurrentChar[Input.charAt(count)]);
Java Code:import java.util.Scanner; public class asst3 { public static void main(String[] args) { System.out.println("Please type a message: \n"); Scanner a = new Scanner(System.in); String Input; while(a.hasNext()) { Input = a.nextLine(); Input = Input.toLowerCase(); int Length = Input.length(); char[] CurrentChar = new char [Length]; for (int count=0; count < Length; count++) { CurrentChar = Input.toCharArray(); if (count == 0 && Character.isLetterOrDigit(Input.charAt(count)) == true) { CurrentChar[count] = Character.toUpperCase(CurrentChar[count]); System.out.printf("%c", CurrentChar[count]); } else if (Character.isLetterOrDigit(Input.charAt(count)) == false && count < Length - 1) { CurrentChar[count+1] = Character.toUpperCase(CurrentChar[count+1]); System.out.printf("%c", CurrentChar[count]); System.out.printf("%c", CurrentChar[count + 1]); count ++ ; } else System.out.printf("%c", CurrentChar[count]); } // for count System.out.println("\n"); } // while hasNext a.close(); } // void main } // class UpperAndLower
Similar Threads
-
java.lang.ArrayIndexOutOfBoundsException: 20
By xminiguyx in forum New To JavaReplies: 1Last Post: 12-14-2011, 04:54 PM -
java.lang.ArrayIndexOutOfBoundsException
By rajasohaibmaroof in forum AWT / SwingReplies: 15Last Post: 09-29-2011, 07:46 PM -
Java.lang.ArrayIndexOutOfBoundsException
By Ladyjest1 in forum New To JavaReplies: 1Last Post: 07-12-2010, 10:11 PM -
java.lang.ArrayIndexOutOfBoundsException
By mensa in forum Java 2DReplies: 7Last Post: 05-05-2008, 09:09 AM -
java.lang.ArrayIndexOutOfBoundsException
By riccian in forum New To JavaReplies: 0Last Post: 03-18-2008, 09:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks