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:
Quote:
Please type a message:
hello world
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 104
at UpperAndLower.main(UpperAndLower.java:39)
Here is my code, I have commented out the lines that cause the program to fail:
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
Edit: After running it another time, apparently my .toLowerCase() function is also not working. Now I'm super boggled.
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
Code:
Input = a.nextLine();
Input.toLowerCase();
but rather:
Code:
Input = a.nextLine();
Input = Input.toLowerCase(); // note the difference!
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.
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. :(snooze):
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.
Quote:
Please type a message:
Hello World
hello Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
at UpperAndLower.main(UpperAndLower.java:45)
Quote:
Originally Posted by
Fubarable
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.
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?
And thank you for your reply!
Re: java.lang.ArrayIndexOutOfBoundsException
Fixed the run time error,
Code:
CurrentChar[count] = Character.toUpperCase(CurrentChar[count]);
instead of
Code:
Character.toUpperCase(CurrentChar[Input.charAt(count)]);
Also fixed a few other array boundary bugs by using a count ++ and a length -1
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