Results 1 to 4 of 4
  1. #1
    aditya86 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Error Exception in thread "main" java.lang.StringIndexOutOfBoundsException

    hi

    I am getting error

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.charAt(String.java:694)
    at IfCounter.main(IfCounter.java:56)

    How to eliminate it.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error Exception in thread "main" java.lang.StringIndexOutOfBoundsException

    Quote Originally Posted by aditya86 View Post
    How to eliminate it.
    Don't try to get a character from a String at position -1.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aditya86 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Error Exception in thread "main" java.lang.StringIndexOutOfBoundsException

    Please Check this code

    findIf = lineOfFile.indexOf("if");
    findWhile = lineOfFile.indexOf("while");
    findElse = lineOfFile.indexOf("else");
    //For if and While
    for(i=0 ; i<2; i++)
    {
    if(i==1)
    findIf = findWhile;
    //If 'if' is found, check to see if 'if' is a an if statement
    if (findIf != -1) {
    precedingChar = lineOfFile.charAt(findIf - 1);

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error Exception in thread "main" java.lang.StringIndexOutOfBoundsException

    Quote Originally Posted by aditya86 View Post
    Please Check this code

    //If 'if' is found, check to see if 'if' is a an if statement
    if (findIf != -1) {
    precedingChar = lineOfFile.charAt(findIf - 1);
    Suppose findIf == 0 ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Exception in thread "main" java.lang.Error ...
    By mossy in forum New To Java
    Replies: 7
    Last Post: 10-31-2010, 09:53 AM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •