Arrays 将文本文件读入数组

Arrays 将文本文件读入数组,arrays,java-io,Arrays,Java Io,我正在尝试将女孩和男孩的名字存储到一个数组中 除了将文件存储到数组中之外,我得到了大部分代码 这就是girls.txt的样子 艾玛125125 伊莱娜415545 金5454 Boys.txt: 德万45645 汤姆4545 克里斯4879797 我需要帮助将文件中的名称和数字存储到数组boynames数组和girlnames数组中。我用代码中的注释显示我需要帮助的地方 import java.io.FileInputStream; import java.io.FileNotFoundExce

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

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

这就是girls.txt的样子

艾玛125125

伊莱娜415545

金5454

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();
}
}
import java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类项目1名称{
公共静态void main(字符串[]args){
扫描仪输入流=空;
字符串[][]男孩姓名=新字符串[1000][2];
字符串[][]girlnames=新字符串[1000][2];
字符串行=null;
布尔值isFoundB=false;
布尔值isFoundG=false;
试一试{
inputStream=新扫描仪(新文件inputStream(“boys.txt”);
}catch(filenotfounde异常){
System.out.println(“打开文件boys.txt时出现问题”);
系统出口(0);
}
扫描仪inputStreamGirls=null;
试一试{
inputStreamGirls=新扫描仪(新文件InputStream(“girls.txt”);
}catch(filenotfounde异常){
System.out.println(“打开文件girls.txt时出现问题”);
系统出口(0);
}
整数计数=0;
同时(计数<1000){
inputStream=boynames[count][0];//此处出错
inputStream=boynames[count][1];//这里
计数++;
}
计数=0;
同时(计数<1000){
inputStreamGirls=girlnames[count][0];//此处
inputStreamGirls=girlnames[count][1];//此处
计数++;
}
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入您想要查找流行程度的名字。\n请确保将名字的第一个字母大写。\n”);
字符串应答=键盘.next();
计数=0;
同时(计数<1000){
如果(男孩姓名[计数][0]==答案){
System.out.println(boynames[count][0]+”在男孩中以“+boynames[count][1]+”命名为“+count+”;
isFoundB=true;
}
if(girlnames[count][0]==答案){
System.out.println(girlnames[count][0]+”在带有“+girlnames[count][1]+”namings”的女孩中排名为“+count+”;
isFoundG=true;
}
计数++;
}
if(isFoundB==false){
System.out.println(答案+“在前1000名男孩名字中没有排名”);
} 
if(isFoundG==false){
System.out.println(答案+“不在前1000名女孩中排名”);
}
inputStreamGirls.close();
inputStream.close();
键盘关闭();
}
}

您需要调用scanner方法来实际读取输入文件

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

因此,与此部分不同:

inputStream =  boynames[count][0]; //Error here
inputStream =  boynames[count][1]; //here
你会做:

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

这里没有问题,谢谢。我不知道为什么我没有想到这一点。这就是我将任何东西存储到变量的方式。不知道为什么我会有不同的想法。必须添加它太像这样的任何一个人看这个代码,以供将来参考。而(count<1000&&inputStream.hasNextLine()){boynames[count][0]=inputStream.nextLine();boynames[count][1]=inputStream.nextLine();count++;}一个问题是它总是返回false而找不到名称。知道为什么吗?您使用的是
nextLine
而不是
next
,这使您可以将整行保存在一个变量中,而不拆分它,因此
boynames[count][0]
将等于
Devan 45645
,而不仅仅是
Devan
。你应该像我的答案中的例子一样使用next。我更改了它,但它仍然总是返回false。它跳过第二个while循环,跳转到if语句,说它没有找到。我想问题出在
boynames[count][0]==answer
。您不能使用==来比较字符串,您需要改为使用
boynames[count][0].equals(answer)
。有关更多信息,请参阅。