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

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default StringIndexOutOfBoundsException

    Ok so basically this code finds all the numbers in a string then prints out how many digits they have...
    The code works fine but if the string has a number at the end it prints out "StringIndexOutOfBoundsException" and as u see I threw that exception in the class but with no use... I don't understand why it gives me that message every time there is a number at the end of the string...


    //Number Finder
    import java.io.*;
    public class pas4{
    	public static void main (String args []) throws IOException, StringIndexOutOfBoundsException{
    		InputStreamReader instream = new InputStreamReader(System.in);
    		BufferedReader br = new BufferedReader(instream);
    		System.out.println("Please insert a passage");
    		String psg = br.readLine();
    		int i = 0;
    		String tmp = "";
    		while (i < psg.length()){
    			while (((psg.charAt(i)) >= 48) && ((psg.charAt(i)) <= 57) && (i < psg.length())){
    				tmp = tmp + psg.charAt(i);
    				i = i + 1;
    			}
    			if (tmp.equals("")){
    				i = i + 1;
    			}else{
    				int z = Integer.parseInt(tmp);
    				int digits = 0;
    				while (z != 0){
    					z = z / 10;
    					digits = digits + 1;
    				}
    				System.out.print(tmp + " has ");
    				System.out.println(digits + " digits ");
    				tmp = "";
    				i = i + 1;
    			}
    		}		
    	}
    }

    Example input: I'll live till 42
    Output: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 17
    at java.lang.String.charAt(String.java:686)
    at pas4.main(pas4.java:34)
    Last edited by Medo Almasry; July 14th, 2011 at 07:15 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: StringIndexOutOfBoundsException

    You need to test that the value of the arg to charAt() is not past the end of the String before you use it:
    (psg.charAt(i)) <= 57) && (i < psg.length())

    Here you use it BEFORE you test it.
    Swap those tests.

    What is the value 57? It would be more readable code if you used the char: '9'

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: StringIndexOutOfBoundsException

    You need to test that the value of the arg to charAt() is not past the end of the String before you use it:
    (psg.charAt(i)) <= 57) && (i < psg.length())

    Here you use it BEFORE you test it.
    Swap those tests.

    What is the value 57? It would be more readable code if you used the char: '9'

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: StringIndexOutOfBoundsException

    Thanks a LOT NORM...
    Never thought java takes care of conditions sequence...

Tags for this Thread