Results 1 to 4 of 4
  1. #1
    Grewe02 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default 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:

    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:

    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
    Edit: After running it another time, apparently my .toLowerCase() function is also not working. Now I'm super boggled.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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
    Java Code:
    Input = a.nextLine();
    Input.toLowerCase();
    but rather:
    Java 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.

  3. #3
    Grewe02 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default 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.

    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.

    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 View Post
    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!

  4. #4
    Grewe02 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Fixed the run time error,
    Java Code:
    CurrentChar[count] = Character.toUpperCase(CurrentChar[count]);
    instead of
    Java Code:
    Character.toUpperCase(CurrentChar[Input.charAt(count)]);
    Also fixed a few other array boundary bugs by using a count ++ and a length -1
    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

  1. java.lang.ArrayIndexOutOfBoundsException: 20
    By xminiguyx in forum New To Java
    Replies: 1
    Last Post: 12-14-2011, 04:54 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By rajasohaibmaroof in forum AWT / Swing
    Replies: 15
    Last Post: 09-29-2011, 07:46 PM
  3. Java.lang.ArrayIndexOutOfBoundsException
    By Ladyjest1 in forum New To Java
    Replies: 1
    Last Post: 07-12-2010, 10:11 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 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
  •