0

I am very new to java and i'm trying to make a basic calculator. There is another question on this but I changed the things that fixed it for him but it still doesn't work. Here's my code:

import java.util.Scanner;  

class HelloWorld{

    public static void main(String args[])

        int num1;
        int num2;
        String op;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your first number");

        num1 = input.nextInt();

        System.out.println("Enter your second number");
        num2 = input.nextInt();

        System.out.println("Enter the operation");
        op = input.nextLine();

        if (op.equals("*")){
            System.out.println("The answer is: " + (num2 * num1));
        }

        if (op.equals("/")){
            System.out.println("The answer is: " + (num2 / num1));
        }

        if (op.equals("+")){
            System.out.println("The answer is: " + (num2 + num1));
        }

        if (op.equals("-")){
            System.out.println("The answer is: " + (num2 - num1));
        }
     }
  }

The error says:

Enter your first number
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at HelloWorld.main(HelloWorld.java:12)

I can't seem to find whats wrong (I have its something simple that i missed)

minipanda1
  • 11
  • 1
  • Why do you need the second `Scanner`? Which is line `12`? – Boris the Spider Dec 22 '13 at 02:43
  • 2
    This error is for `main()` method of `HelloWorld` application. – PM 77-1 Dec 22 '13 at 02:45
  • I don't think i do. I got rid of it but it gives me the same error. – minipanda1 Dec 22 '13 at 02:46
  • See if you're **closing** scanner anywhere in your code. – PM 77-1 Dec 22 '13 at 02:47
  • The code you have posted is **not** the code generating that stack trace. – Brian Roach Dec 22 '13 at 02:50
  • I'm pretty shure you would get the wrong results for non-commutative operations (i.e. -, /) even if you solve the error, since you exchanged the operands. (You want to input A before B for calculating A / B , right?) – fabian Dec 22 '13 at 02:57
  • As an aside, after you figure out what's causing the exception, you might want to give http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx a read, *especially* if you end up with a program that is "not reading in the inputted operation". – Dennis Meng Dec 22 '13 at 03:15
  • possible duplicate of [NoSuchElementException with Java.Util.Scanner](http://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner) – Alex K Dec 22 '13 at 12:11

4 Answers4

2

What are you entering?

Enter your first number
10
Enter your second number
20
Enter the operation
*
The answer is: 200
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

I'm not sure your program would even compile the way you have it, but here's one problem:

You didn't open your main method with an open curly brace.

Change this:

public static void main(String args[])

to this:

public static void main(String args[]) {

When I tested your code, it terminated after Enter the operation. However, when I changed this line:

op = input.nextLine();

to this:

op = input.next();

It worked perfectly.

Here is an example of the console input/output:

Enter your first number
10
Enter your second number
20
Enter the operation
*
The answer is: 200
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

Only thing I see wrong is public **satic** void main(String args[]) Other than that it's doing what it should...

bimm3rBoy
  • 43
  • 7
  • okay yeah that was me typing it in (I accidentally deleted that after coping the code in) – minipanda1 Dec 22 '13 at 02:59
  • The OP says that they're getting at least the first line to print out to the console. If this `satic`/`static` thing were the problem, the program wouldn't even compile. – Kyle Falconer Dec 22 '13 at 02:59
0

Whenever, you read any token from Scanner. You have the facility to check whether it really exists with hasNext() methods. So, you can do

For checking ints :

if(input.hasNextInt())
input.nextInt()

For checking lines :

if(input.hasNextLine())
input.nextLine()

This will get rid of the NoSuchElementException

Update:

 System.out.println("Enter the operation");
 op = input.next(); //changing it to next() fixed it

Output

 run:
    Enter your first number
    2
    Enter your second number
    3
    Enter the operation
    *
    The answer is: 6
    BUILD SUCCESSFUL (total time: 6 seconds)
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Not in this case. `Ststem.in` stream always has `next`. – PM 77-1 Dec 22 '13 at 03:33
  • You mean System.in? Can you please explain a bit? – Keerthivasan Dec 22 '13 at 03:35
  • `hasNext()` will always return `true` when reading from `System.in`, since there is no such thing as the end of file for keyboard input. Obviously, the same is not true for `hasNextInt()` or any other type-specific method. – PM 77-1 Dec 22 '13 at 03:40
  • Thanks for the point on *System.in*. if the same is not true for hasNextInt() method, then when can we use it? – Keerthivasan Dec 22 '13 at 03:43