0

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);
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
imtinan
  • 191
  • 12
  • 2
    Add example input, leading to this exception, to create [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Currently code works fine and issue can't be reproduced. – Chaosfire Dec 03 '22 at 08:06
  • 1
    As a side note, since ctor parameter is named `Date`, `this.date=date;` does nothing. Either change parameter name do `date`, or change assigning the value to `this.date = Date;`. The first option should be prefered since java variable names start with lower case. – Chaosfire Dec 03 '22 at 08:08
  • Does this answer your question? [java.util.NoSuchElementException: No line found](https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found). Search for more. – Ole V.V. Dec 03 '22 at 11:03
  • From where are you reading input? Which line is line 24 of your program? The stack trace says the exception is thrown there. – Ole V.V. Dec 03 '22 at 11:13
  • your error is on line `this.date = Date;` you have a parameter named `date`. Variable `Date` basically doesn't exist. – m3ow Dec 03 '22 at 11:25
  • 1
    @m3ow It’s the other way around. The OP has — by mistake, I suppose — named the constructor parameter `Date` with a capital `D`. And no matter which way, this error cannot cause `java.util.NoSuchElementException: No line found`. – Ole V.V. Dec 03 '22 at 12:20
  • [I cannot reproduce](https://ideone.com/lQXucd) – Ole V.V. Dec 03 '22 at 12:52
  • @OleV.V. I see what you mean, I had a wrong observation. – m3ow Dec 03 '22 at 16:07
  • 1
    I tried reproducing the error. It might be caused by your input. Say, for example, when I input a ctrl+z on a `nextLine()` it throws NoSuchElementException. – m3ow Dec 03 '22 at 16:11

1 Answers1

1

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).

jurez
  • 4,436
  • 2
  • 12
  • 20