Jonathan Dewein
Jonathan Dewein

Reputation: 984

FileWriter not receiving all input and mysterious dangling newline issue

I am trying to write a Java program in which the user specifies how many "student records" they would like to input, followed by the student's name, age, and GPA, which then gets stored as text. However, I am having a problem with my text not including all entered data and a mysterious dangling newline that I cannot get rid of. Here is my program:

import java.io.*;
import java.util.Scanner;

public class CreateFile {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        FileWriter fwriter = new FileWriter("c:\\Students.dat");
        PrintWriter StudentFile = new PrintWriter(fwriter);
        String name = " ";
        String next = " ";
        int age = 0;
        int hm = 0;
        double gpa = 0.0;
        System.out.print("How many student records would you like to enter: ");
        hm = input.nextInt();
        for (int x = 1; x <= hm; x++) {
            System.out.print("Enter Name: ");
            name = input.nextLine();
            input.nextLine();
            System.out.print("Enter Age: ");
            age = input.nextInt();
            System.out.print("Enter GPA: ");
            gpa = input.nextDouble();
            next = input.nextLine();
            StudentFile.println(name);
            StudentFile.println(age);
            StudentFile.println(gpa);
        }
        StudentFile.close();
        System.exit(0);
    }
}

Here is sample input and output to illustrate my issues:

run:
How many student records would you like to enter: 3
Enter Name: Jon
Enter Age: 20
Enter GPA: 3.4
Enter Name: Bill

Enter Age: 24
Enter GPA: 3.6
Enter Name: Ted

Enter Age: 34
Enter GPA: 3.9

This is the produced text file:
20
3.4
Bill
24
3.6
Ted
34
3.9

Why doesn't it store the first name entered? Why isn't there a newline in the first entry, but it is in the others?

Upvotes: 0

Views: 62

Answers (3)

Markus L
Markus L

Reputation: 1026

This is the corrected for loop:

for ( int x = 1; x <= hm; x++ )
{
     System.out.print( "Enter Name: " );
     name = input.next();
     input.nextLine();
     System.out.print( "Enter Age: " );
     age = input.nextInt();
     input.nextLine();
     System.out.print( "Enter GPA: " );
     gpa = input.nextDouble();
     next = input.nextLine();
     StudentFile.println( name );
     StudentFile.println( age );
     StudentFile.println( gpa );
}

Some things you may want to consider:

  • Handle the IOException - it should not be ignored!!
  • Use the methods hasNextXXX() of the Scanner to check if something is available.
  • Refactor your usage of the variable next, it's never really used.
  • It's not necessary to call System.exit( 0 ) from the main method - rather use the return statement with a meaningful value.

Upvotes: 1

Niklas P
Niklas P

Reputation: 3517

The other examples only work if you don't have names like "James Peter" - in their code examples only James would be saved as name.

I'd prefer this:

    System.out.print("How many student records would you like to enter: ");
    hm = input.nextInt();
    input.nextLine();
    for (int x = 1; x <= hm; x++) {
        System.out.print("Enter Name: ");
        name = input.nextLine();
        System.out.print("Enter Age: ");
        age = input.nextInt();
        input.nextLine();
        System.out.print("Enter GPA: ");
        gpa = input.nextDouble();
        input.nextLine();
        StudentFile.println(name);
        StudentFile.println(age);
        StudentFile.println(gpa);
    }

Upvotes: 1

TurnipEntropy
TurnipEntropy

Reputation: 527

The problem is that you're using nextLine() when you need to be using next(). I'm assuming you put the second input.nextLine() in there because you were initially having a problem where it would print out "Enter Name: " and then immediately "Enter Age: ". nextLine() is telling your program to skip whatever is there, and not to wait for it. The reason that this paradigm worked at all for any of your entries is that you put next = input.nextLine() at the bottom of your loop. Here's a fix:

package createfile;
import java.io.*;
import java.util.Scanner;
public class CreateFile {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        FileWriter fwriter = new FileWriter("c:Students.dat");
        PrintWriter StudentFile = new PrintWriter(fwriter);
        String name = " ";
        String next = " ";
        int age = 0;
        int hm = 0;
        double gpa = 0.0;
        System.out.print("How many student records would you like to enter: ");
        hm = input.nextInt();
        for (int x = 1; x <= hm; x++) {
            System.out.print("Enter Name: ");
            name = input.next();
            System.out.print("Enter Age: ");
            age = input.nextInt();
            System.out.print("Enter GPA: ");
            gpa = input.nextDouble();
            StudentFile.println(name);
            StudentFile.println(age);
            StudentFile.println(gpa);
        }
        StudentFile.close();
        System.exit(0);
    }
}

You could also just move your input.nextLine() above name=input.nextLine() and it would have the same effect.

Upvotes: 1

Related Questions