+ 2
NoSuchElementException on scan.nextLine();
I'm getting nextLine() is throwing NoSuchElementException no line found even though the syntax is right and I just want to take a string from user What i did is Scanner scan = new Scanner(System.in); String choice = scan.nextLine();
14 Réponses
+ 3
nextLine return String object.
Now you can use parseXXX method from respective type to parse the string for example
int x=Integer.parseInt(y);
double z=Double.parseDouble(y);
+ 3
Generally such exceptions are arisen because java cannot find more inputs from scanner. It is NOT A PROBLEM IN YOUR CODE. You can just check if the user have inputted any value by using scan.hasNext()
Another way to get away from this exception is catching it simply.
eg:
try {
var choice = scan.nextLine();
} catch (NoSuchElementException e) {
System.out.print("please enter a value"); //something like this 😉
}
+ 1
Thanks for your reply
Sorry but there's no such question
I'm the first one to ask
+ 1
And what i did was I've created a calculator problem upon execution it asks user whether to do another operation or not that's why I need to take a string input from user but I'm getting this error
N i didn't closed the scanner anywhere but if i comment the doCal method then the scanner works as expected but not with doCal
+ 1
Any code to show ? It's hard to tell where the error is
+ 1
package basic;
public class MyCalc {
public static void main(String[] args)
{
Cal.op();
System.out.println("Another operation ??");
String choice = Cal.scan.nextLine();
System.out.println("you entered in main "+choice);
switch(choice)
{
case "yes":
case "y":
case "yup":
Cal.op();
System.out.println(choice);
break;
default:
System.out.println("Ok quitting ! have a nice day !!!");
}
}
}
I've declared the scanner object scan as static in Cal class
Everything works as expected but when the control reaches to the switch statement here it always executes default condition
N if i add a System.out.println(choice) in default case then nothing prints
+ 1
It just asks two double variables and prints the operation
+ 1
But the problem isn't with Cal.op
Coz if i comment the switch block the rest calculator works as expected
+ 1
package basic;
import java.util.Scanner;
public class Cal {
static Scanner scan = new Scanner(System.in);
public static void op()
{
System.out.println("which operation you want to perform ??");
String operation = scan.nextLine();
doCal(operation);
}
public static void doCal(String operation)
{
Calculator calc = new Calculator();
System.out.println("the option u entered is : "+operation);
System.out.println("Enter a number :");
double a = Cal.scan.nextDouble();
System.out.println("Enter second number :");
double b = Cal.scan.nextDouble();
try {
switch(operation)
{
case "addition":
calc.add(a, b);
break;
case "substraction":
calc.sub(a, b);
break;
case "multiplication":
calc.mul(a, b);
break;
case "division":
calc.div(a, b);
break;
default:
throw new MyExceptions("Error enter valid option : "+operation +"is not a valid option");
}
+ 1
catch(MyExceptions ex)
{
System.out.println("\n-------------------------------------------------------------");
System.out.println("enter valid option :"+operation +" is not a valid option");
//ex.getMessage();
System.out.println("-------------------------------------------------------------");
op();
}
}
}
0
What about in the op method ?
0
Just in case, show us Cal class, you call it twice in your code
0
Strange design, which line is throwing the error ?
Btw dont mix nextline with nextdouble, it'll cause a trouble because the nextline will take new line character from the previous nextdouble (it could be why the switch directly go to default, but nothinh shown), instead keep using nextLine but parse the input as double
0
Then how do i take double , int , string inputs from same scanner object!??