将文本文件读入数组

Reading text files into array

我正在尝试将女孩和男孩的名字存储到一个数组中。

除了将文件存储到数组中之外,我得到了大部分代码。

这就是girls.txt的样子

艾玛 125125

伊莱娜 415545

金 545454

Boys.txt:

德文 45645

汤姆 4545

克里斯 4879797

我需要帮助将文件中的姓名和号码存储到数组 boynames 数组和 girlnames 数组中。我在代码

中显示我需要评论的地方
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Project1Names {
   public static void main(String[] args) {
    Scanner inputStream = null;
    String[][] boynames = new String[1000][2];
    String[][] girlnames = new String[1000][2];
    String line = null;
    boolean isFoundB = false;
    boolean isFoundG = false;
    try {
        inputStream = new Scanner(new FileInputStream("boys.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file boys.txt");
        System.exit(0);
    }

    Scanner inputStreamGirls = null;
    try {
        inputStreamGirls = new Scanner(new FileInputStream("girls.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file girls.txt");
        System.exit(0);
    }
       int count = 0;
        while (count < 1000){
            inputStream =  boynames[count][0]; //Error here
            inputStream =  boynames[count][1]; //here
            count++;
        }

        count = 0;

        while (count < 1000 ){
            inputStreamGirls = girlnames[count][0]; //here
            inputStreamGirls = girlnames[count][1]; //here
            count++;
        }
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter the first name that you would like to find the popularity of.\n Be sure to capitalize the first letter of the name.\n");
      String answer = keyboard.next(); 

        count = 0;
        while(count < 1000){
            if (boynames[count][0] == answer){
                System.out.println(boynames[count][0] + " is ranked " + count + " among boys with " +  boynames[count][1] +  " namings");
                isFoundB = true;
            }
            if (girlnames[count][0] == answer){
                System.out.println(girlnames[count][0] +  " is ranked " + count +  " among girls with " + girlnames[count][1] + " namings");
                isFoundG = true;
            }
            count++;
        }

        if(isFoundB == false){
            System.out.println(answer + " is not ranked among the top 1000 boy names.");
        } 
        if(isFoundG == false){
            System.out.println(answer + " is not ranked among the top 1000 girl names.");
        }

    inputStreamGirls.close();
    inputStream.close();
    keyboard.close();
}
}

您需要调用扫描器方法才能实际读取输入文件。

scanner.next() 从输入中读取一个字符串标记。

所以代替这部分:

inputStream =  boynames[count][0]; //Error here
inputStream =  boynames[count][1]; //here

你会做:

boynames[count][0] = inputStream.next();
boynames[count][1] = inputStream.next();