Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: String Index out of range

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default String Index out of range

    Hey, stumped as to why this is wrong. Its the start of a program that will count the number of vowels in a sentance, and I've hit a hurdle. Once I can overcome this the rest of the program should be ok.

    Any idea why its wrong?

    import javax.swing.JOptionPane;
    public class PracticeString
    {
    	public static void main(String Args[])
    	{
    		String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
     
    		int UserInputLength = UserInput.length();
    		int Loop = 0;
    		int ACount = 0;
    		int ECount = 0;
    		int ICount = 0;
    		int OCount = 0;
    		int UCount = 0;
    		char Vowel;
    		int testing = 0;
     
    		while (Loop <= UserInputLength)
    		{
    			for (Loop = 0;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if (UserInput.charAt(Loop)== 'A' )
    				{
    					ACount++;
    				}
     
    			}
     
    		} 
     
    		System.out.println("The total number of A's are " +ACount);
     
    	}
    }

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(String.java:687)
    at PracticeString.main(PracticeString.java:22)

    Process completed.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String Index out of range

    Basically, the last index of the String is the size of the String minus 1. So, you do not want to go while Loop is less than OR EQUAL TO the length of the String, but rather just less than it.

    Look at the word String.
    The size of String is 6, but the index range of String is 0-5

    Also on that note, why do you have the FOR Loop nested in the WHILE loop? That doesn't accomplish anything since the WHILE loop will quit on the condition that the FOR loop is met...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    javapenguin (November 8th, 2010), petemyster (November 8th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String Index out of range

    Quote Originally Posted by aussiemcgr View Post
    Basically, the last index of the String is the size of the String minus 1. So, you do not want to go while Loop is less than OR EQUAL TO the length of the String, but rather just less than it.

    Look at the word String.
    The size of String is 6, but the index range of String is 0-5

    Also on that note, why do you have the FOR Loop nested in the WHILE loop, that doesn't accomplish anything since the WHILE loop will quit on the condition that the FOR loop is met...
    It made sense in my head. I works, thank you very much for helping me with the problem, you were right and once I just subtracted Loop by 1, it worked. Here is the finished code incase anyone is interested.

    import javax.swing.JOptionPane;
    public class PracticeString
    {
    	public static void main(String Args[])
    	{
    		String UserInput= JOptionPane.showInputDialog("Enter a sentance: ");
     
    		int UserInputLength = UserInput.length();
    		int Loop = 0;
    		int ACount = 0;
    		int ECount = 0;
    		int ICount = 0;
    		int OCount = 0;
    		int UCount = 0;
    		char Vowel;
    		int testing = 0;
     
    		while (Loop <= UserInputLength)
    		{
    			for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if ((UserInput.charAt(Loop-1)== 'A') || (UserInput.charAt(Loop-1)== 'a') )
    				{
    					ACount++;
    				}
    			}
    			for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if ((UserInput.charAt(Loop-1)== 'E') || (UserInput.charAt(Loop-1)== 'e') )
    				{
    					ECount++;
    				}
    			}
    			for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if ((UserInput.charAt(Loop-1)== 'I')|| (UserInput.charAt(Loop-1)=='i') )
    				{
    					ICount++;
    				}
    			}
    			for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if ((UserInput.charAt(Loop-1)== 'O')|| (UserInput.charAt(Loop-1)== 'o') )
    				{
    					OCount++;
    				}
    			}
    				for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
    			{
    				if ((UserInput.charAt(Loop-1)== 'U') || (UserInput.charAt(Loop-1)== 'u'))
    				{
    					UCount++;
    				}
     
    			}
    		}
    		System.out.println("The total number of A's are " +ACount);
    		System.out.println("The total number of E's are " +ECount);
    		System.out.println("The total number of I's are " +ICount);
    		System.out.println("The total number of O's are " +OCount);
    		System.out.println("The total number of U's are " +UCount);
    		System.out.println("The original sentance was: " +UserInput);
     
    	}
    }

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String Index out of range

    For your reference, you can do this a few other ways as well.

    One way would be by using a method:
    import javax.swing.JOptionPane;
     
    public class JunkTesting
    {
     
        public static void main(String[] args)
        {
        	String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
     
    		//Run method for both uppercase and lower case chars
    		int ACount = numberOfTimes(UserInput,'a') + numberOfTimes(UserInput,'A');
    		int ECount = numberOfTimes(UserInput,'e') + numberOfTimes(UserInput,'E');
    		int ICount = numberOfTimes(UserInput,'i') + numberOfTimes(UserInput,'I');
    		int OCount = numberOfTimes(UserInput,'o') + numberOfTimes(UserInput,'O');
    		int UCount = numberOfTimes(UserInput,'u') + numberOfTimes(UserInput,'U');
     
    		System.out.println("The total number of A's are " +ACount);
    		System.out.println("The total number of E's are " +ECount);
    		System.out.println("The total number of I's are " +ICount);
    		System.out.println("The total number of O's are " +OCount);
    		System.out.println("The total number of U's are " +UCount);
    		System.out.println("The original sentance was: " +UserInput);
        }
     
        public static int numberOfTimes(String s,char c)
        {
        	int count = 0;
        	for(int i=0;i<s.length();i++)
        	{
        		if(s.charAt(i) == c)
        			count++;
        	}
        	return count;
        }
    }

    Another way would be to use one loop:
    import javax.swing.JOptionPane;
     
    public class JunkTesting
    {
     
        public static void main(String[] args)
        {
        	String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
     
    		//Run method for both uppercase and lower case chars
    		int ACount = 0;
    		int ECount = 0;
    		int ICount = 0;
    		int OCount = 0;
    		int UCount = 0;
     
    		for(int i=0;i<UserInput.length();i++)
    		{
    			if(UserInput.charAt(i)=='a' || UserInput.charAt(i)=='A')
    				ACount++;
    			else if(UserInput.charAt(i)=='e' || UserInput.charAt(i)=='E')
    				ECount++;
    			else if(UserInput.charAt(i)=='i' || UserInput.charAt(i)=='I')
    				ICount++;
    			else if(UserInput.charAt(i)=='o' || UserInput.charAt(i)=='O')
    				OCount++;
    			else if(UserInput.charAt(i)=='u' || UserInput.charAt(i)=='U')
    				UCount++;
    		}
     
    		System.out.println("The total number of A's are " +ACount);
    		System.out.println("The total number of E's are " +ECount);
    		System.out.println("The total number of I's are " +ICount);
    		System.out.println("The total number of O's are " +OCount);
    		System.out.println("The total number of U's are " +UCount);
    		System.out.println("The original sentance was: " +UserInput);
        }
    }

    There are tons of other ways as well, those are just to first two that come to mind for me.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    petemyster (November 8th, 2010)

Similar Threads

  1. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  2. [SOLVED] Help; algorithm to determine 'range'
    By b_jones10634 in forum Algorithms & Recursion
    Replies: 13
    Last Post: August 24th, 2010, 04:57 PM
  3. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM
  4. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM
  5. set the slider's models (range)
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2009, 11:59 PM