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: Error with String range print -1.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error with String range print -1.

    Scanner input = new Scanner(System.in);

    int x = 0;

    System.out.println(" Enter a math operation: ");
    String expr = input.next();

    int pos = expr.indexOf("+");

    if ( pos >= 0) {
    int op1 = Integer.parseInt(expr.substring(0, pos));
    int op2 = Integer.parseInt(expr.substring( pos - x , expr.length()));
    System.out.println( op1 + op2);
    } else
    if (pos == expr.indexOf("-")) {
    int op1 = Integer.parseInt(expr.substring(0, pos));
    int op2 = Integer.parseInt(expr.substring( pos - x , expr.length()));
    System.out.println( op1 - op2);
    } else
    if (pos == expr.indexOf("/")) {
    int op1 = Integer.parseInt(expr.substring(0, pos));
    int op2 = Integer.parseInt(expr.substring( pos - x , expr.length()));
    System.out.println( op1 / op2);
    } else
    if (pos == expr.indexOf("%")) {
    int op1 = Integer.parseInt(expr.substring(0, pos));
    int op2 = Integer.parseInt(expr.substring( pos - x , expr.length()));
    System.out.println( op1 % op2);
    }
    else {
    System.out.println(" Invalid operation ");
    }
    }
    }
    ALthough this gets compiled, the program automatically says

    an ex out of range: -1
    at java.lang.String.substring(String.java:1937)
    at MathFun.main(MathFun.java:21)
    Is there something wrong with the loop? I just want to make a simple loop that lets the user enter a string 2+2 and let the loop read that out as a string and print the result. Thank You!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Error with String range print -1.

    int pos = expr.indexOf("+");
    What happens when the user enters an expression that is not addition? The indexOf method returns -1 when it cannot find the target char.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error with String range print -1.

    How would I make it so it finds it instead? In other words, how would I initialize it? I'm sorry if this is confusing, but I don't understand this much.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Error with String range print -1.

    You need to rework your if statements. All the other (- / %) are kinda on the right track but you are comparing each value returned from indexOf to pos. Get rid of pos and compare the returned value to .....

    I leave that as exercise for you to think about. Hint: how does the first if statement work?
    Improving the world one idiot at a time!

Similar Threads

  1. String Index Out of range
    By Shikha Jain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 10:14 PM
  2. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  3. String index is out of range
    By etag in forum Exceptions
    Replies: 9
    Last Post: April 4th, 2012, 10:59 PM
  4. String index out of range?
    By oksmartypants in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 02:10 PM
  5. String Index out of range
    By petemyster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 03:59 PM

Tags for this Thread