Reputation: 181
I am tryin to take user input in a parameterized java constructor but I am failing. It gives the following error
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Main.main(Main.java:24)
Here is my code
import java.util.Scanner;
class Student
{
String name;
String date;
Student( String name, String Date)
{
this.name=name;
this.date=date;
}
}
public class Main
{
public static void main(String args[])
{
System.out.println("Here is the date");
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String name = myObj.nextLine();
System.out.println("Enter date");
String date = myObj.nextLine();
Student s1= new Student(name,date);
}
}
Upvotes: 0
Views: 96
Reputation: 4667
According to your stack trace, this has nothing to do with constructors. The error happens when Scanner
tries to read a line from the standard input.
If you are running this program in IDE, the input via System.in
might not be available, so there is no next line for Scanner
to read. Try running your program from console / command line. Some IDEs also have a checkbox for enabling standard input (usually as part of run/debug configuration).
Upvotes: 1