我正在尝试将女孩和男孩的名字存储到一个数组中。
除了将文件存储到数组中之外,我获得了大部分代码。
这就是girls.txt的样子
艾玛 125125
伊莱娜 415545
金 545454
男孩.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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句