Reputation: 55
I am trying to make a calculator with a certain input that is entered like + 5 5 or / 10 2. When I compile and run it I get this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at calc.main(calc.java:11)
My Code
import java.util.Scanner;
public class calc {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
String calc;
double num1;
double calcdu = 0.0;
double num2;
while ( true ) {
calc = input.next();
num1 = input.nextDouble();
num2 = input.nextDouble();
if (calc.equals("+"))
{
calcdu= num1 + num2;
System.out.printf("%.2e %s %.2e %c %.2e\n", num1, "+", num2, '=', calcdu);
}
if (calc.equals("/"))
{
calcdu=num1/num2;
System.out.printf("%.2e %s %.2e %c %.2e\n", num1, "/", num2, '=', calcdu);
}
if (calc.equals("-"))
{
calcdu=num1-num2;
System.out.printf("%.2e %s %.2e %c %.2e\n", num1, "-", num2, '=', calcdu);
}
if (calc.equals("*"))
{
calcdu=num1*num2;
System.out.printf("%.2e %s %.2e %c %.2e\n", num1, "*", num2, '=', calcdu);
}
if (calc.equals("%"))
{
calcdu=num1%num2;
System.out.printf("%.2e %s %.2e %c %.2e\n", num1, "%", num2, '=', calcdu);
}
}}}
Upvotes: 3
Views: 133
Reputation: 4006
You get the error because there is no tokens in the Scanner for the function to obtain. You should check that there are tokens left in the Scanner with the Scanner.hasNext()
function.
There is also an easier way to make calculations based on user input, using a JavaScript engine, like so:
public static void main (String[] args) throws java.lang.Exception
{
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
Scanner kbd = new Scanner(System.in);
String operator = kbd.next();
double number1 = kbd.nextDouble();
double number2 = kbd.nextDouble();
System.out.println(engine.eval(number1 + operator + number2));
}
For this to work you will need to add import javax.script.*;
to the top of your java file, along with the other imports.
Upvotes: 0
Reputation: 328
The NoSuchElementException occurs when a class tries to get an element or token and there aren't any there. For example, if the Scanner class is trying to get the next element from standard input and there's nothing there (empty or null). This can happen if the user doesn't enter anything or the program terminates prematurely.
You can avoid this by first checking that the user has entered something before moving on with your program.
For the Scanner class, this can be done using the hasNext() method or some variation of it.
You can read more about it here
As an example:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String operator = "";
if (input.hasNext()) { // Check that Scanner has input
operator = input.next();
}
while (true) {
if (operator.isEmpty()) { // No value entered, program can't continue
System.out.println("No value entered, program will now exit.");
break;
}
}
}
Upvotes: 0
Reputation: 3024
This exception occurs when there are no more tokens in scanner
, and you try to get next, without checking. See NoSuchElementException from Java docs.
In your case, as Ramanlfc
mentioned, you are calling Next()
on scanner without checking with if next
even exists. You can use hasNext()
to check if next exists.
Upvotes: 1
Reputation: 8354
Your calling next()
on scanner
without checking with hasNext()
while(input.hasNext())
{
num1 = Double.parseDouble(input.nextDouble());
num2 = Double.parseDouble(input.nextDouble());
......//further code
Upvotes: 1