0

这是我正在开发的锻炼应用程序。这个想法是创建一个帐户(文本文件)并将您输入的数据存储在其中。这是我到目前为止所拥有的。

public static void main(String[] args) {

    boolean start = true;

    while (start == true) {

        CreateNewMember();
        start = DecideToAddOrQuit();
    }
}


public static void CreateNewMember() {

    Scanner keyboard = new Scanner(System.in);
    out.println("Enter a username: ");
    String input = keyboard.nextLine();
    createMember member = new createMember(input);
    member.setMembership();
    member.setInfo();
}

public static boolean DecideToAddOrQuit() {
    Scanner keyboard = new Scanner(System.in);
    out.println("\nPress 1 if you want to continue adding data.");
    out.println("Press any other key if you want to leave.");

    String decision = keyboard.nextLine();
    if (decision.equals("1")) {
        out.println("");
        return true;
    } else {
        out.println("Goodbye!");
        return false;
    }
}

这是负责将数据添加到文件的类:

public class createMember {

public String name;
private String fullName;
private String age;
private String experience;

private Formatter x;

public createMember(String name) {
    this.name = name;
}

public void setMembership() {
    try {           
        x = new Formatter(name);   
        out.println("File with name \"" + name + "\" has been created!");
    } catch (Exception e) {
        out.println("Could not create username.");
    }
}

public void setInfo () {
    Scanner keyboard = new Scanner(System.in);
    String fullNameIn, ageIn, experienceIn;

    out.println("Enter your Full Name");
    fullNameIn = keyboard.nextLine();
    fullName = fullNameIn;
    out.println("Enter your Age");
    ageIn = keyboard.nextLine();
    age = ageIn;
    out.println("Enter your lifting experience\n");
    experienceIn = keyboard.nextLine();
    experience = experienceIn;
    x.format("%s\t%s\t%s", fullName, age, experience );
 }

}

我输入的值(全名、年龄、经验)未存储在用户名文件中。我该如何解决这个问题,为什么会发生?

4

2 回答 2

3

如果您希望在程序运行时写入输出,您可能看不到它,因为Formatter 缓冲了输出

您必须close()在完成文件 ( x.close()) 后写入文件,以便写入任何剩余的缓冲数据(这不会自动发生,即使在程序退出时也是如此)。此外,如果您希望立即实际写入输出,flush()则只要写入一行 ( x.flush()) 即可。

于 2013-11-05T21:30:29.220 回答
0

这个问题不是最清楚的,但就我而言,您根本没有告诉过任何实际将数据写入文件的内容。你应该使用PrintWriter. 这会将您想要的数据加载到文件中。另外,在您的代码中,您的文件永远不会被创建。

import java.io.PrintWriter;

public class createMember {

public String name;
private String fullName;
private String age;
private String experience;

// private Formatter x; not quite sure what this does. There seems to be no need for it.

PrintWriter write;

File f;    

public createMember(String name) {
    this.name = name;
}

public void setMembership() throws Exception {
    try {           
        // x = new Formatter(name);
        f = new File (name);
        f.createNewFile(); // This throws a HeadlessException (i believe, as it might be a FileNotFoundException instead)
        out.println("File with name \"" + name + "\" has been created!");
    } catch (Exception e) {
        out.println("Could not create username.");
    }
}

public void setInfo () throws Exception {
    Scanner keyboard = new Scanner(System.in);
    String fullNameIn, ageIn, experienceIn;
    write = new PrintWriter(f.toString()); // This throws a FileNotFoundException
    out.println("Enter your Full Name");
    fullNameIn = keyboard.nextLine();
    fullName = fullNameIn;
    write.println (fullName);
    out.println("Enter your Age");
    ageIn = keyboard.nextLine();
    age = ageIn;
    write.println(age);
    out.println("Enter your lifting experience\n");
    experienceIn = keyboard.nextLine();
    experience = experienceIn;
    write.println(experience);
    //x.format("%s\t%s\t%s", fullName, age, experience );
    write.close(); // Be sure to close it, or it gives a warning and the file is never written.
 }

}

我希望这是您正在寻找的,并且编码愉快!

于 2013-11-05T21:40:13.483 回答