+ 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();

27th Nov 2018, 11:12 AM
Madhusudan Babar
Madhusudan Babar - avatar
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);
27th Nov 2018, 1:10 PM
Taste
Taste - avatar
+ 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 😉 }
28th Nov 2018, 1:23 PM
Seniru
Seniru - avatar
+ 1
Thanks for your reply Sorry but there's no such question I'm the first one to ask
27th Nov 2018, 11:43 AM
Madhusudan Babar
Madhusudan Babar - avatar
+ 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
27th Nov 2018, 11:46 AM
Madhusudan Babar
Madhusudan Babar - avatar
+ 1
Any code to show ? It's hard to tell where the error is
27th Nov 2018, 11:47 AM
Taste
Taste - avatar
+ 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
27th Nov 2018, 11:52 AM
Madhusudan Babar
Madhusudan Babar - avatar
+ 1
It just asks two double variables and prints the operation
27th Nov 2018, 12:06 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 1
But the problem isn't with Cal.op Coz if i comment the switch block the rest calculator works as expected
27th Nov 2018, 12:07 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 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"); }
27th Nov 2018, 12:21 PM
Madhusudan Babar
Madhusudan Babar - avatar
+ 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(); } } }
27th Nov 2018, 12:23 PM
Madhusudan Babar
Madhusudan Babar - avatar
0
What about in the op method ?
27th Nov 2018, 12:05 PM
Taste
Taste - avatar
0
Just in case, show us Cal class, you call it twice in your code
27th Nov 2018, 12:20 PM
Taste
Taste - avatar
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
27th Nov 2018, 1:03 PM
Taste
Taste - avatar
0
Then how do i take double , int , string inputs from same scanner object!??
27th Nov 2018, 1:04 PM
Madhusudan Babar
Madhusudan Babar - avatar