Java - 如何根据请求打印文件中的数据?

标签 java file oop methods

这是一个锻炼程序,它会询问用户的姓名并创建一个文件来存储他的所有数据。在该文件中,有 3 个文字:全名、年龄和经验。

当用户输入用户名时,不起作用的功能是打印出文件内的所有信息。

所以,如果我有一个名为 Bob 的帐户,并且我在控制台中输入 Bob,我应该得到我的全名、我的年龄和我的经验。 (之前已经存储在文件中)。

这是我从文件中读取数据并将其打印出来的方法。我用调试器运行了几次,但它只读取文件的标题,而不读取其中的信息。结果是“找不到文件”。我该如何解决?谢谢。

 public void getInfo(String nameIn) {
    Scanner keyboard = new Scanner(System.in);
    Scanner x;
    out.println("\nWhat's your account's name?");
    nameIn = keyboard.nextLine();

    //It reads the title of the file, not the data inside it.
    try {
        x = new Scanner(nameIn);
        if (x.hasNext()) {
            String a = x.next();
            String b = x.next();
            String c = x.next();
            out.println("Account data for user: " + nameIn);
            out.printf("Name: %s \tAge: %s \tExperience: %s", a, b, c);
        }
    } catch (Exception e) {
        out.println("Could not find file.");
    }

这是该类中的其余代码。

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

    out.println("Enter your Full Name");
    fullName = keyboard.nextLine();
    out.println("Enter your Age");
    age = keyboard.nextLine();
    out.println("Enter your lifting experience");
    experience = keyboard.nextLine();
    x.format("Name: %s \tAge: %s \tExperience: %s", fullName, age, experience);
}

最佳答案

public void getInfo(String nameIn) {
    Scanner keyboard = new Scanner(System.in);
    Scanner x;
    System.out.println("\nWhat's your account's name?");
    nameIn = keyboard.nextLine();

//It reads the title of the file, not the data inside it.
try {
    File file = new File("nameOfYourFile.txt");
    x = new Scanner(file);
    while (x.hasNextLine())) {
        String line = x.nextLine();

        if (line.contains(nameIn)){  // or you can use startsWith() 
                                    // depending how your text is formatted
            String[] tokens = line.split(" ");
            String a = tokens[0].trim();
            String b = tokens[1].trim();
            String c = tokens[2].trim();
            System.out.println("Account data for user: " + nameIn);
            System.out.printf("Name: %s \tAge: %s \tExperience: %s", a, b, c);

        }
    }
} catch (FileNotFoundException e) {
    System.out.println("Could not find file.");
}

关于Java - 如何根据请求打印文件中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19813194/

相关文章:

java - 在文件夹中创建 .asm 文件作为 JMenuItems 并将它们添加到 JMenu,添加 ActionListener 事件

java - 依赖倒置原则(适用于 Java)

javascript - 如何将对象方法传递给javascript中的数组方法

java - 使用 Java Runtime 类在 IE 中打开带有请求参数的 URL

java - 如何使用嵌套数组解析 JSON 到对象

c - 为什么“while(!feof(file))”总是错误的?

c# - 抛出的异常未在 UnitTest try/catch 中捕获

java - 如何在 Excel 中取消扩展警告

java - 在loop/try/catch java中使用外部变量

java - 如何从 Android 中的 URI 获取文件?