Results 1 to 2 of 2
  1. #1
    schaem is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default Exception in thread “main” java.lang.StringIndexOutOfBoundsException:..

    This is my code:

    import java.util.*;
    import dataobjects.*;

    public class StackApplication {

    public static void main (String args []){

    List myList = new List();
    Stack myStack = new Stack();

    System.out.println ("Enter an expression in Reverse Polish Notation: ");
    Scanner sc = new Scanner (System.in);
    String userInput = sc.nextLine();

    System.out.println (userInput);
    System.out.println (userInput.length());

    for (int i=0; i<userInput.length();i++)
    {
    if (userInput.charAt(i) != ' '){
    myList.append(new AnyClass (userInput.charAt(i)));
    }
    }

    myList.showList();

    int j=0;

    while ((userInput.charAt(j) != '+') || (userInput.charAt(j) != '-') || (userInput.charAt(j) != '*') || (userInput.charAt(j) != '/') )
    {
    myStack.push(new AnyClass (userInput.charAt(j)));
    j++;
    }

    }
    }

    and I am getting the following error, the error seems to be coming from the while loop but I cannot figure out what is wrong with it:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(String.java:658)
    at StackApplication.main(StackApplication.java:39)
    Java Result: 1

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

    Default Re: Exception in thread “main” java.lang.StringIndexOutOfBoundsException:..

    Suppose a character is a +; it most certainly isn't a -, so your while clause succeeds; (the while condition is incorrect).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 9
    Last Post: 07-16-2012, 03:42 AM
  2. Replies: 5
    Last Post: 04-22-2012, 04:59 PM
  3. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 04:19 PM
  4. Replies: 1
    Last Post: 11-23-2011, 05:52 AM
  5. Replies: 3
    Last Post: 09-11-2011, 09:15 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
  •