FileWriter没有收到所有输入和神秘的悬空换行问题

时间:2016-11-07 17:00:32

标签: java io filereader filewriter

我正在尝试编写一个Java程序,其中用户指定他们想要输入多少“学生记录”,然后是学生的姓名,年龄和GPA,然后将其存储为文本。但是,我的文字问题不包括所有输入的数据和一个我无法摆脱的神秘悬空换行。 这是我的计划:

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

以下是示例输入和输出以说明我的问题:

{{1}}

为什么不存储输入的名字?为什么在第一个条目中没有换行符,但它在其他条目中?

3 个答案:

答案 0 :(得分:1)

这是针对循环的更正:

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 );
}

您可能需要考虑的一些事项:

  • 处理IOException - 不应该忽略!!
  • 使用hasNextXXX()的{​​{1}}方法检查某些内容是否可用。
  • 重构您对变量Scanner的使用,它从未真正使用过。
  • 没有必要从主方法调用next - 而是使用具有有意义值的System.exit( 0 )语句。

答案 1 :(得分:1)

问题是,当您需要使用nextLine()时,您正在使用next()。我假设你把第二个input.nextLine()放在那里,因为你最初遇到的问题是打印出“输入名称:”然后立即“输入年龄:”。 nextLine()告诉你的程序跳过那里的任何东西,而不是等待它。这个范例对你的任何条目都有用的原因是你将next = input.nextLine()放在循环的底部。这是一个修复:

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);
    }
}

您也可以将input.nextLine()移到name=input.nextLine()之上,效果会相同。

答案 2 :(得分:1)

其他示例仅在您没有“James Peter”这样的名称时才有效 - 在他们的代码示例中,只有James会被保存为名称。

我更喜欢这个:

    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);
    }