因此,这个项目的基础是扫描一个包含名称的文件,然后在过去的一个世纪中每十年对其进行一次普及。我试图实现一个HashMap,但它似乎没有认识到名称就在那里(它是)。
import java.util.*;
import java.io.*;
/**
* Write a description of class NameRecord here.
*
* @author (your name)
* @version (a version number or a date)
* Implement a hashmap to seperate the name from the numbers to allow it to check.
*/
public class NameRecord
{
ArrayList<Integer> namesList = new ArrayList<Integer>();
HashMap<String, ArrayList> combined = new HashMap<String, ArrayList>();
public void storeNames()
{
try
{
Scanner scan = new Scanner(new File("names.txt"));
while(scan.hasNextLine())
{
String firstName = scan.next();
while(scan.hasNext())
{
int rank = scan.nextInt();
namesList.add(rank);
}
combined.put(firstName, namesList);
}
}
catch(Exception e)
{
}
}
public void nameRecord()
{
System.out.println("What name would you like to search?");
Scanner scan2 = new Scanner(System.in);
String name = scan2.nextLine();
System.out.println("\nThe name you chose to search was: " + name);
if(combined.containsKey(name))
{
System.out.println("This name appears in the database");
}
else
{
System.out.println("This name does not appear in the database");
}
}
}