java - 为什么我的程序无法识别文本文件中的所有名称?

标签 java file-io text-processing

我是一名 CS210 学生,在检查文本文件中是否有多个姓名以从中提取数据时遇到问题。我将以下数据存储在文本文件中。它是一个名称,后跟 11 个整数,用于从中读取数据:

-Sally 0 0 0 0 0 0 0 0 0 0 886 

-Sam 58 69 99 131 168 236 278 380 467 408 466

-Samantha 0 0 0 0 0 0 272 107 26 5 7

-Samir 0 0 0 0 0 0 0 0 920 0 798

以下代码适用于组中的名字 (Sally),并且运行正确。但该程序不会向 Sam、Samantha 或 Samir 的控制台返回任何内容。我不确定为什么它对一个人可以正常工作,但对其他人却不起作用。

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

public class TestSpace1 {
    public static void main(String[] args) throws FileNotFoundException{

    Scanner fileInput = new Scanner(new File("names.txt"));
    String nameUsed = nameSearch();
    dataScan(fileInput, nameUsed);

}

public static void dataScan(Scanner file, String name) {
    String nameCheck = file.next();

        try{
            // While there are still tokens to be read in the file
            while(!name.equals(file.next())) {
                while(file.hasNext()) {
                // If the name is equal to the next String token in the file
                    if (nameCheck.equals(name)) {

                        // Initialize variable to contain start year
                        int year = 1910;

                        // Run through for loop to add up the integers and display them in value pairs for the year
                        for (int i = 0; i < 11; i++) {
                            int ranking = file.nextInt();
                            System.out.println(year + ": " + ranking);
                            year += 10;

                        }

                      // If the file doesn't read the name, loop to the end of the line, start at top of loop
                    } else {

                        file.nextLine();


                    }

                }
            }
            // If there is a line out of bounds, catch exception
        } catch (Exception e) {
            System.out.println("All lines have been read");
        }

}

public static String nameSearch() {
    Scanner input = new Scanner(System.in);


    System.out.println("This program allows you to search through the");
    System.out.println("data from the Social Security Administration");
    System.out.println("to see how popular a particular name has been");
    System.out.println("since 1900.");

    System.out.print("What name would you like to search? ");
    String name = input.nextLine();

    return name;
}

}

最佳答案

问题是 nameCheck 没有更新为文件中的下一个标记。您需要将 nameCheck 移动到 while 循环内,以便它更改每个循环。另外,您也不需要 while(!name.equals(file.next()))) 因为如果名称匹配是 file.next ,循环将终止,而不是计算总和整数。该代码适用于第一个,因为它是列表中的第一个,因此 nameCheck 不需要更新,并且 file.next() 将是下一个而不是第一个。您的代码应如下所示:

try{
    while(file.hasNext()){
        String nameCheck = file.next();
            if (nameCheck.equals(name)){
                //code for calculating sum
             }
    }
 } 

关于java - 为什么我的程序无法识别文本文件中的所有名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50429716/

相关文章:

java - ORA-12505,TNS :listener does not currently know of SID

java - 使用线程中的 LOCATION_SERVICE

java - 如何使用 Java(无 DB)更快地删除文件中的重复/聚合行

c++ - 将数据实时写入文件或在程序关闭时写入文件更好吗?

Java:处理文本行

java - 可执行文件的 Java 文件路径中的空格

C - 使用 .txt 文件作为输入时遇到无限循环

text - 使用 `awk` 在 BEGIN 部​​分打印文件中的行数

python - 元组列表中元组的第一个元素小写

Java:实例变量与局部参数