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 bounds exeption when attempting to read a string char by char

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default string index out of bounds exeption when attempting to read a string char by char

    this is a homework project that i have reached a dead end with.
    the object of the program is to read a file, scan each line, cheack for brackets and then number them matching each opening and closing bracket.
    however, when it reaches the first bracket, it runs a exception and i can not figure it out.
    here is the main loop.

    while(inPut.hasNextLine()){
    isbracket =inPut.nextLine();

    for (int i= 0; i <=isbracket.length() ; i++){
    checkIt = Character.toString(isbracket.charAt(i));
    System.out.print(CheckBracket(checkIt));}
    }
    }

    here is the method i call to check for brackets

    public static String CheckBracket(String checkIt){
    int numBracket = 0;
    String Checked ="";
    if (checkIt.equals("{")){
    numBracket++;
    Checked = (checkIt) +numBracket;}
    if (checkIt.equals("}") && numBracket > 0){
    Checked = checkIt +Integer.toString(numBracket);
    numBracket--;}
    if (checkIt.equals("}") && numBracket == 0){
    Checked = (checkIt) + numBracket;}
    if (checkIt != null) {Checked = checkIt;}
    return Checked;

    this is the error
    {java.lang.StringIndexOutOfBoundsException: String index out of range: 12
    at java.lang.String.charAt(Unknown Source)
    at MatchUp2.main(MatchUp2.java:40)

    (line 40 is: checkIt = Character.toString(isbracket.charAt(i))
    can some one please just point me in the right direction?


  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: string index out of bounds exeption when attempting to read a string char by char

    java.lang.StringIndexOutOfBoundsException: String index out of range: 12
    at java.lang.String.charAt(Unknown Source)
    at MatchUp2.main(MatchUp2.java:40)
    How long is the String being scanned on line 40? The index is past the end of the String.
    Remember: indexes range in value from 0 to the length-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: string index out of bounds exeption when attempting to read a string char by char

    lol that had JUST occured to me as well, i have fixed that by subtracting one from the isbracket.length(). now however i have another problem, i can either make it print the whole file in one line, or i can make it print it one work per a line. any ideas how to make it keep the strings intact?

  4. #4
    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: string index out of bounds exeption when attempting to read a string char by char

    how to make it keep the strings intact?
    What do you mean by "intact"? What is the program doing that is a problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  2. Go through string one char at a time
    By joshft91 in forum Java Theory & Questions
    Replies: 7
    Last Post: April 18th, 2011, 12:19 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. 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
  5. Need help with concatenizing char? to string?
    By bh-chobo in forum Java SE APIs
    Replies: 3
    Last Post: October 16th, 2009, 08:40 AM