0

I am making a basic calculator. When I try to compare Strings and I use next(), it works fine, however if I use nextLine(), it doesn't work. How can this be? Aren't next and nextLine practically the same thing, just that one skips a line and one doesn't?

Here is my code:

import java.util.Scanner;

class apples{

    public static void main(String args[]){

        Scanner Max = new Scanner(System.in);

        int num1, num2, answer;
        String plumi;

        System.out.println("enter your first number");
        num1 = Max.nextInt();
        System.out.println("enter your second number");
        num2 = Max.nextInt();
        System.out.println("enter if you want to use plus or minus");
        plumi = Max.next();
        if(plumi.equals("plus")){
            answer = num1 + num2;
            System.out.println("the answer is " + answer);
        }
        if(plumi.equals("minus")){
            answer = num1 - num2;
            System.out.println("the answer is " + answer);
        }

    }
}
haylem
  • 22,460
  • 3
  • 67
  • 96
emileber
  • 23
  • 4
  • 5
    You could try debugging it to see the strings you are comparing. – The Cat Jun 11 '13 at 15:02
  • `nextLine` reads from the current position until the end of the line – Aleks G Jun 11 '13 at 15:03
  • 1
    If you are not using a library (for example, Apache commons lang) to provide null safe string comparison, invert your string compairsons to be "literal".equals(not_literal) or in your case "plus".equals(plumi) – DwB Jun 11 '13 at 15:04
  • 2
    first thing : Java naming convention says variables and instances need to have the first letter lowercase. What do you mean by "it doesn't work"? what happen? as for `next()` vs `nextLine()`, the first one only gets up to the next space while `nextLine` gets up to the \n break – TecHunter Jun 11 '13 at 15:04
  • Please don't start variables with uppercase, it's confusing in Java. Only classes start with uppercase. – m0skit0 Jun 11 '13 at 15:05
  • I'm trying your code -- it works for me. – Alex D Jun 11 '13 at 15:06
  • What was your exact input into the scanner with `nextLine()`? I presume "plus" or "minus" but I just thought I ought to make sure. – Quetzalcoatl Jun 11 '13 at 15:13
  • possible duplicate of [Reading Strings next() and nextLine() Java](http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java) – Josh Lee Jun 11 '13 at 15:14
  • http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Josh Lee Jun 11 '13 at 15:15

4 Answers4

3

They’re not the same.

The next() and nextInt() methods first skip any input that matches the delimiter pattern, and then attempt to return the next token. The nextLine() method returns the rest of the current line.

For example, if the input is "123\nplus\n", a call to nextInt() will consume the 123, leaving the \n in waiting.

At this point, a call to next() will skip the \n, then consume the plus, leaving the final \n in waiting. Alternatively, a call to nextLine() will consume the \n and return an empty string as the line, leaving the plus\n in waiting.

The answer, if you wish to use nextLine() after using next() or nextInt(), is to insert an additional call to nextLine() to flush the newlines that were left over.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
1

Try this code instead of yours:

if(plumi.equals("plus")){
    answer = num1 + num2;
    System.out.println("the answer is " + answer);
}
else if(plumi.equals("minus")){
    answer = num1 - num2;
    System.out.println("the answer is " + answer);
}
else {
    System.out.println(plumi);
}

And then try following input:

1 //press enter
2 plus //press enter

See what happens and you will understand it.

darijan
  • 9,725
  • 25
  • 38
0

The reason one works and the other doesn't is... well, they aren't the same thing. They both exist for slightly different use cases.

Compare next() and nextLine() - nextLine() is expecting a line separator to terminate on which I presume your input doesn't have. However, the doc comment suggests it should work even without the terminating line separator, so you'll have to debug to find out exactly why it breaks for you.

Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
-1

At first glance, your code should work. To see why it doesn't work, you will have to debug it. If you don't know how to use a debugger, yet, use the "poor man's debugger":

System.out.println("enter if you want to use plus or minus");
plumi = Max.next();
System.out.println("You entered ["+plumi+"]"); // poor man's debugger

I'm quoting the value with [] because they are rarely part of the value that I want to print and it makes it easier to see when there are additional, unexpected whitespaces (like [ plumi] or [plumi ])

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Not sure why this was downvoted... to be fair, it would prove the answer. The result would have a new line somewhere in it and provide some insight. –  Jun 11 '13 at 15:30