1

Sorry if this is a dumb question, I'm a bit of a programming noob and am having trouble with the following code.

public static void main(String[] args) throws IOException {
    addName();
    addName();      //Line "10" in error
}

public static void addName() throws IOException {
    //Initializing Variables and Scanner
    Scanner keyboard = new Scanner(System.in);
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    keyboard.close();
    System.out.println("Data written to the file.");;
}

The addName method works when run once, but I get the follow error when running the second time.

How many people do you wish to add? 1
Enter the first name of person 1: s
Enter the last name of person 1: d
Enter the email of person 1: g
Data written to the file.
How many people do you wish to add? Exception in thread "main" 
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Problem1.addName(Problem1.java:25)
at Problem1.main(Problem1.java:10)

I originally thought this was from a newline character that wasn't consumed somewhere, but I can't seem to figure it out. Why am I getting this error? How can I fix it? Thank you!

Tobin
  • 19
  • 1

1 Answers1

2

You shouldn't close the Scanner at the end of your method, and ideally you should reuse the same Scanner during the entire execution.

By closing the Scanner, you are also closing its input source, which is System.in in this case. Therefore, nothing can be inputted by the user anymore.

Below is a corrected snippet:

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    addName(keyboard);
    addName(keyboard);
}

public static void addName(Scanner keyboard) throws IOException {
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    System.out.println("Data written to the file.");
}
Dici
  • 25,226
  • 7
  • 41
  • 82
  • Wow, I feel dumb now. Thank you so much! – Tobin Nov 26 '17 at 22:10
  • It's not necessarily intuitive that closing the reader also closes the input source, but as you become more experienced you'll realize this is generally the default behaviour in Java (I don't actually know any counter-example in the standard library). Feel free to upvote/accept this answer if it was helpful. – Dici Nov 26 '17 at 22:11
  • The principle is, you're responsible for closing anything you opened. So, you opened file `Data.txt`; you're responsible for closing it. You did *NOT* open `System.in`; you only wrapped a `Scanner` around it, but it was already opened for you by Java (or, really, the operating system). So you should not be closing it. – David Conrad Nov 26 '17 at 22:46