0

I have the following Classes:

public class Human {
    private String firstName;
    private String lastName;

    public Human(){
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter Firstname");
        setFirstName(in.nextLine());
        System.out.println("Please enter Lastname");
        setLastName(in.nextLine());
        in.close();
    }
}

And

public class Customer extends Human{
    private long customerNumber;
    private String customerCompanyName;
    static long customerNumberCounter = 0;

    public Customer(){
        super();
        Scanner in = new Scanner(System.in);
        setCustomerNumber(customerNumberCounter);
        customerNumberCounter ++;
        System.out.println("Please enter Companyname");
        setCustomerCompanyName(in.nextLine());
        in.close();
    }
}

When i call the following Main Method:

public static void main(String[] args) {
    Customer aCustomer = new Customer();
    System.out.println(aCustomer.giveFullName());
}

The Programm throwes the following Error:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at customers.Customer.(Customer.java:17) at customers.Customer.main(Customer.java:22)

I don't understand what I am doing wrong, since I can create an Object of Class Human without any problems but when I create a Customer, the Error pops up directly after entering the LastName in the Human Constructor.

I already renamed the Scanner and so on but that didn't solve the problem.

Anybody got a Solution for this?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Snackaholic
  • 590
  • 7
  • 27
  • Which one of the two scanner is reading your typing when you get to read the company name? I wouldn't like to use two different scanner. Just create one and get a ref to it for both classes. – Jkike Aug 07 '15 at 07:46
  • Use buffer reader in place of scanner it is more reliable. Use its readLine() method and also InputStreamReader with system.in, system.in is used to read data from console. – Ankur Mahajan Aug 07 '15 at 07:46
  • @ Jkike each Constructor has his own Scanner – Snackaholic Aug 07 '15 at 07:48
  • take a look at the answer here: http://stackoverflow.com/questions/12124793/java-scanner-nextline-not-waiting-for-input – Moh-Aw Aug 07 '15 at 07:54
  • 2
    I don't know if mark this question as duplicated, but your answer is [here](http://stackoverflow.com/a/13042296/3998458), the main problem is because when you close your scanner the first time, it close `System.in` too and apparently it can't be reopen... You should instanciate this object and close it only at the end. – Alex S. Diaz Aug 07 '15 at 08:01
  • @ Alexandro im creating a new Scanner object in each constructor! But i will give it a try – Snackaholic Aug 07 '15 at 08:14
  • super wierd, but it works this way! but the compiler now wants me to close the scanner in the human constructor... well if i do so it doesnt work anymore :D – Snackaholic Aug 07 '15 at 08:18
  • @TeaTime closing the first scanner also closes `System.in` (the underlying stream). compiler warnings are OK in this case. – Moh-Aw Aug 07 '15 at 08:43

1 Answers1

0

Before read the next,you close the scanner.So it can not read the line:

public Human(){
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter Firstname");
    setFirstName(in.nextLine());
    System.out.println("Please enter Lastname");
    setLastName(in.nextLine());
    //in.close();
}
Beniamin
  • 56
  • 2