Error in java Exception in thread "main"

0

The error it presents is:

  

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

I have a readAddress method that gets the values of the class. This method is called from another class Employee to read Address of Employee among other attributes. I attach the two methods. When I test the methods separately it works well.

public void readAddress() {

    Scanner read = new Scanner(System.in);
    String string;

    System.out.println("Address(Separate each data with '/'): ");
    string = read.nextLine();

    String[] substring = string.split("/");

    this.building = substring[0];
    this.apartment = substring[1];
    this.street = substring[2];
    this.city = substring[3];
    this.province = substring[4];
    this.code = substring[5];
    read.close();
}

public void readEmployee() {

    Scanner read = new Scanner(System.in);


    System.out.println("Last name: ");
    this.lastname = read.nextLine();
    System.out.println("First name: ");
    this.firstname = read.nextLine();
    this.email.readEmail();
    this.address.readAddress();
    this.telephone.readTelephone();
    System.out.println("Social security number: ");
    this.ssn = read.nextLine();
    read.close();
}

The error is on the line:

string = read.nextLine();
    
asked by Juan Carlos Marmolejo Martínez 21.10.2017 в 23:56
source

1 answer

0

The java api says about the readLine () method the following:

  

Throws:       NoSuchElementException - if no line was found You are supposed to handle this exception or just use the hasNextLine () method to avoid   the exception.

You're supposed to handle this exception or simply use the hasNextLine () method to avoid the exception.

while(read.hasNextLine()){
    ssn= read.nextLine();
}
    
answered by 22.10.2017 в 00:15