This question already has answers here:
Scanner NoSuchElementException
(2 answers)
Closed 7 years ago.
I am executing the below code to read a string from the input stream with the scanner object.
public class Test{
public static void main(String[] args) {
String userInput1 = getUserInput();
System.out.println(userInput1);
String userInput2 = getUserInput();
System.out.println(userInput2);
}
private static String getUserInput() {
System.out.println("Enter the String");
Scanner scanner = new java.util.Scanner(System.in);
String input = scanner.next();
scanner.close();
return input;
}
}
The first invocation to the method getUserInput succeeded without any issues.But he second invocaton threw NoSuchElementException.
Enter the String
test1
test1
Enter the String
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
There are two problems with the code
It assumes that there already has been input. A simple while(!scanner.hasNext()){} before the locations where you call scanner.next(); should work.
You are creating many scanners (this may or may not impact the code, but does impact efficiency)
Instead, the Scanner scanner = new Scanner(System.in); can be broken up into a declaration (Scanner scanner;) outside a method and then defined (scanner = new Scanner(System.in);) in the main method or initialization code.
Related
This question already has answers here:
Java Scanner "unassigned closeable value" is never closed [Eclipse]
(2 answers)
Closed last year.
Why is vs code giving me a warning on this:
(The warning is below the code)
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String input = sc.nextLine();
System.out.println(input);
}
}
The warning is:
Resource leak: 'sc' is never closed
You can simply put sc.close() and this error will no more be displayed. It is not a compulsion. Without this statement also program works fine but if your eyes are getting disturbed by again and again popping u of the message of error then you can put sc.close(). Note that after you put it, you can not use sc to get input after that statement.
To sort this error:
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String input = sc.nextLine();
System.out.println(input);
sc.close();
}
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Whenever I use sc.nextLine in java I get this error. It skips one line. Anybody knows how to fix it. sc.nextLine fails for multiple test cases as takes skips one line and then take input. Anybody knows another way to take sentence input in java?
For Input:
2
geeksforgeeks
geeks for geeks
your output is:
geeksforgeeks
Code:
class GFG {
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
//code
}
}
you have to read a line after int testcases=sc.nextInt(); because reading the Int from scanner is not reading a line Termination chars....
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
sc.nextLine(); //<--HERE!
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
I'm studying Java classes and I'm trying to create a code where the user inputs how many objects (in this case "cube") they want to create.
In my main class I have this code written
System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
objNumInput.close();
Cube cubes[] = new Cube[objNum];
for (int i = 0; i < objNum; i++){
String cubeName = Cube.inputName();
double cubeLength = Cube.inputLength();
cubes[i] = new Cube(cubeName, cubeLength);
}
in my Cube class I have here:
public static String inputName(){
String cubeName;
Scanner input = new Scanner(System.in);
System.out.println("Enter the name: ");
cubeName = input.nextLine();
return cubeName;
}
public static double inputLength(){
double cubeLength;
Scanner input = new Scanner(System.in);
System.out.println("Enter the length: ");
cubeLength = input.nextDouble();
return cubeLength;
}
When I run it, I can input the number of "cubes" I want to create. Then, it keeps throwing an exception
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Cube.inputName(Cube.java:40)
at Main.main(Main.java:88)
what's wrong?
Do not close your Scanner, it will close System.in as well.
When a Scanner is closed, it will close its input source if the source implements the Closeable interface
As I understand (correct me if I'm wrong) the reason why you close your objNumInput is that you want to use it in two different methods.
I would suggest you to pass the Scanner as input parameter into your methods inputName and inputLength. Then you'll be able to reuse the same scanner without closing it in between.
public static String inputName(Scanner scanner){
String cubeName;
System.out.println("Enter the name: ");
cubeName = scanner.nextLine();
return cubeName;
}
public static double inputLength(Scanner scanner){
double cubeLength;
System.out.println("Enter the length: ");
cubeLength = scanner.nextDouble();
return cubeLength;
}
...
System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
//objNumInput.close(); <-- Do not close the scanner
Cube cubes[] = new Cube[objNum];
for (int i = 0; i < objNum; i++){
String cubeName = Cube.inputName(objNumInput);
double cubeLength = Cube.inputLength(objNumInput);
cubes[i] = new Cube(cubeName, cubeLength);
}
put objNumInput.close(); after for loop in your main method.The reason your program flashes by without pausing the second time because System.in is closed when you do objNumInput.close(); in the line number 3 of main method
closing a Scanner object will close the underlying stream.
-your code only works one time because System.in is getting closed. You cannot "open" System.in again. A closed stream cannot be reopened
This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
how to take more inputs from user using Nextline() in java
if i use scn.Nextline() after reading one input it will work but giving a null in answer.
public class Mapdemo {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
System.out.println("enter the number of phoneaddress you want to store");
int n=scn.nextInt();
int j=0;
System.out.println("enter the phonenumber first and then name of the person phoneaddress");
Map<String, Long> m1=new HashMap<String,Long>();
for(int i=0;i<n;i++)
{
Long l=scn.nextLong();
String s=scn.nextLine();
m1.put(s,l);
}
System.out.println("enter the name to be checked");
String s2=scn.next();
System.out.println(m1.get(s2));
After using .nextInt() for the first time do:
scanner.nextLine();
after that to clear the line, nextLine() stops at the newline character (OS dependant).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()
I have a problem with the scanner. When I compile it, there are no problems. but when I want to run this program, I get an exception. Can any of you explain me the reason of this problem?
import java.util.Scanner;
public class CiagArytmetyczny {
public static void main(String[] args) {
Scanner s = new Scanner("System.in");
System.out.println("Podaj dlugosc ciagu: ");
int dl = s.nextInt();
int element = 2;
for(int i=1; i<=dl; i++) {
element=element+3;
System.out.println(element);
}
}
}
Podaj dlugosc ciagu:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at CiagArytmetyczny.main(CiagArytmetyczny.java:8)
Process completed.
You have a problem in this line
Scanner s = new Scanner("System.in");
You are passing a string to the Scanner constructor. According to the java docs (Scanner(String source)), a new Scanner that produces values scanned from the specified string will be returned. According to the rest of your program, a String with a number should be provided for the scanner to pick up in the following line.
int dl = s.nextInt();
If you intend to get input from the console, Please change the scanner initialization as follows.
There are few more constructors to Scanner, I suggest you have a look at the java docs.
Scanner s = new Scanner(System.in);
This will give the console input stream to the Scanner.