这是一个使用哈希映射的简单程序,其中使用排名系统从名称为“ babynameranking2008.txt”的文件中为男婴和女婴的名字命名。在我的变量情况下,我的切换案例收到了空指针异常
仅在男孩和女孩的每种切换情况下的变量中都发生错误,例如:“ ranking = boys [index] .get(name);”。如果我将排名等于0,则会删除该异常。
public class Lab08 {
@SuppressWarnings("unchecked")
private static Map<String, Integer>[] boys = new HashMap[10];
@SuppressWarnings("unchecked")
private static Map<String, Integer>[] girls = new HashMap[10];
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String answer;
readNames(); // method to read the info from the files and add to our Map array
// Get user input and continue until done
do {
System.out.print("ENter a year (2008-2017): ");
int year = input.nextInt();input.nextLine();
System.out.print("Boy or girl? ");
String sex = input.nextLine().toLowerCase();
System.out.print("ENter a name: ");
String name = input.nextLine();
sex = sex.toLowerCase();
int index = 0;
int ranking = 0;
switch (sex) {
// display the output based on the sex (boy or girl)
case "boy":
index = year - 2008;
ranking = boys[index].get(name);
System.out.printf("The Boy named %s is ranked %d in %d \n",name,ranking,year);
break;
case "girl":
index=year-2008;
ranking=girls[index].get(name);
System.out.printf("The Boy named %s is ranked %d in %d \n",name,ranking,year);
break;
default:
System.out.println("sex should be boy or girl");
break;
}
//input.close();
System.out.print("Do you want check another name (yes or no)?");
answer = input.nextLine().toLowerCase();
} while (answer.equals("yes"));
} // end main
//read information from each file and add to appropriate Map array
public static void readNames() throws IOException {
String fixedName="babynameranking";
int year=2008;
String type=".txt";
File infile;
for(int i=0;i<10;++i) {
boys[i] = new HashMap<>();
girls[i] = new HashMap<>();
}
for(int i=0;i<=9;++i) {
// construct the file name
String filename = fixedName+Integer.toString(year)+type;// construct the file name ;
infile = new File(filename);
Scanner in = new Scanner(infile);
while(in.hasNext()) {
// read info from the file and add to Map arrays
int ranking=in.nextInt();
String boyName=in.next();
in.next();
String girlName=in.next();
in.next();
//put in map
boys[i].put(boyName, ranking);
girls[i].put(girlName, ranking);
} // end while
in.close();
year=year+1;
} // end for loop
} // end readNames()
} //结束Lab08
答案 0 :(得分:-1)
问题是您没有包括文件的路径。因此,抛出 FileNotFoundException。
在此处插入文件的路径:
String filename = fixedName + Integer.toString(year) + type;
将其更改为以下内容:
String filename="/Users/../Desktop/babynameranking.txt";
而且您也不需要这些变量:
String fixedName="babynameranking";
int year=2008;
String type=".txt";