myles438a
myles438a

Reputation: 31

Writing to file and looping (Loop not performing correctly)

The program works like this -

  1. Creates a file writer and scanner.
  2. Creates a loop (to loop 3 times).
  3. Asks some questions to collect data from the user.
  4. Writes all of the collected data to the blank text file and loops.

My problem is that after the first loop, the program asks two of the questions instead of one by one and doesn't allow you to answer one of them.

Here is my code -

package Week19;

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;

public class CreatingFiles {
public static void main(String[] args) throws Exception {

    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("textfile.txt")));
    Scanner scanner = new Scanner (System.in);

    for(int i=0; i < 3; i++){

        System.out.println("Please enter a name please");
        String employeeName = scanner.nextLine();

        System.out.println("Please enter a job title");
        String employeeJobTitle = scanner.nextLine();

        System.out.println("Please enter a age");
        while(!scanner.hasNextInt()) {
            scanner.next();
        }
        int employeeAge = scanner.nextInt();

        System.out.println("Please enter a salary");
        while(!scanner.hasNextDouble()) {
            scanner.next();
        }
        Double employeeSalary = scanner.nextDouble();

        writer.println("Employee Name: "+ employeeName);
        writer.println("Employee Job Title: "+ employeeJobTitle);
        writer.println("Employee Age: "+ employeeAge);
        writer.println("Employee Salary: "+ employeeSalary);



    }
        writer.close();
        scanner.close();


}
}

The error:

Error I keep receiving

Upvotes: 2

Views: 148

Answers (1)

Lubo Kanev
Lubo Kanev

Reputation: 480

To sum up the answer: after a scanner.nextDouble() there's a new line that remains. Get rid of it by calling scanner.nextLine(). Heres the code:

package Week19;

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;

public class CreatingFiles {
public static void main(String[] args) throws Exception {

    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("textfile.txt")));
    Scanner scanner = new Scanner (System.in);

    for(int i=0; i < 3; i++){

        System.out.println("Please enter a name please");
        String employeeName = scanner.nextLine();

        System.out.println("Please enter a job title");
        String employeeJobTitle = scanner.nextLine();

        System.out.println("Please enter a age");
        while(!scanner.hasNextInt()) {
            scanner.next();
        }
        int employeeAge = scanner.nextInt();

        System.out.println("Please enter a salary");
        while(!scanner.hasNextDouble()) {
            scanner.next();
        }
        Double employeeSalary = scanner.nextDouble();
        scanner.nextLine();

        writer.println("Employee Name: "+ employeeName);
        writer.println("Employee Job Title: "+ employeeJobTitle);
        writer.println("Employee Age: "+ employeeAge);
        writer.println("Employee Salary: "+ employeeSalary);
    }
    writer.close();
    scanner.close();
}

}

Upvotes: 2

Related Questions