Java 将.txt文件读入Hashmap并对其进行操作
我最大的问题很可能是没有完全理解Hashmaps以及如何操作它们,尽管我看了一些教程。希望你们这些聪明的灵魂能给我指出正确的方向 我正在尝试将.txt文件读入hashmap。文本文件包含2006年人名的流行情况。inputFile的每一行都包含一个男孩的名字和一个女孩的名字,以及有多少人被命名。例如:1 Jacob 24797 Emily 21365将是第1行文件的输入 我想把男孩的名字放在一个列表中,女孩的名字放在第二个列表中,保持他们当前的位置,这样用户就可以搜索jacob,并被告知这是当年的第一个男孩名字,依此类推其他名字。之前,我只是逐行阅读文件,看看文件中包含了我要搜索的名称。这是可行的,但它无法分辨这是男孩的名字还是女孩的名字,这导致了一个错误,如果我说我在搜索雅各布对女孩有多受欢迎,它仍然会说第一。我认为hashmap是解决这个问题的最佳方法,但实际上无法让它工作 我的代码Java 将.txt文件读入Hashmap并对其进行操作,java,file-io,hashmap,Java,File Io,Hashmap,我最大的问题很可能是没有完全理解Hashmaps以及如何操作它们,尽管我看了一些教程。希望你们这些聪明的灵魂能给我指出正确的方向 我正在尝试将.txt文件读入hashmap。文本文件包含2006年人名的流行情况。inputFile的每一行都包含一个男孩的名字和一个女孩的名字,以及有多少人被命名。例如:1 Jacob 24797 Emily 21365将是第1行文件的输入 我想把男孩的名字放在一个列表中,女孩的名字放在第二个列表中,保持他们当前的位置,这样用户就可以搜索jacob,并被告知这是当年
public void actionPerformed(ActionEvent e)
{
//Parse Input Fields
String name = inputArea.getText();
if (name.equals(""))
{
JOptionPane.showMessageDialog(null, "A name is required.", "Alert", JOptionPane.WARNING_MESSAGE );
return;
}
String genderSelected = genderList.getSelectedItem().toString();
String yearSelected = yearList.getSelectedItem().toString();
String yearFile = "Babynamesranking"+yearSelected+".txt"; //Opens a different name file depending on year selection
boolean foundName = false;
Map<String, String> map = new HashMap<String,String>(); //Creates Hashmap
try
{
File inputFile = new File(yearFile); //Sets input file to whichever file chosen in GUI
FileReader fileReader = new FileReader(inputFile); //Creates a fileReader to open the inputFile
BufferedReader br = new BufferedReader(fileReader); //Creates a buffered reader to read the fileReader
String line;
int lineNum = 1; //Incremental Variable to determine which line the name is found on
while ((line = br.readLine()) != null)
{
if (line.contains(name))
{
outputArea.setText(""+name+" was a popular name during "+yearSelected+".");
outputArea.append("\nIt is the "+lineNum+" most popular choice for "+genderSelected+" names that year.");
foundName = true;
}
String parts[] = line.split("\t");
map.put(parts[0],parts[1]);
lineNum++;
}
fileReader.close();
}
catch(IOException exception)
{
exception.printStackTrace();
}
String position = map.get(name);
System.out.println(position);
}
您需要两个哈希图,一个用于男孩的名字,一个用于女孩的名字-目前您使用男孩的名字作为键,女孩的名字作为值,这不是您想要的。相反,使用两个
Map
数据结构,其中String
是名称,IntTuple
是行号(秩)和使用此名称的人数
class IntTuple {
final int rank;
final int count;
IntTuple(int rank, int count) {
this.rank = rank;
this.count = count;
}
}
问题是,通过使用
if (line.contains(name))
您正在检查整行中是否存在该名称,关于它是男孩的名称还是女孩的名称。您可以分别读取它们,然后决定要检查哪个值。您可以这样做:
while ((line = br.readLine()) != null)
{
Scanner sc = new Scanner(line);
int lineNumber = sc.nextInt();
String boyName = sc.next();
int boyNameFreq = sc.nextInt();
String girlName = sc.next();
int girlNameFreq = sc.nextInt();
if(genderSelected.equals("male") && name.equals(boyName)){
// .. a boy's name is found
}
else if(genderSelected.equals("female") && name.equals(girlName)){
// .. a girl's name is found
}
}
Scanner类用于解析该行,并逐个标记地读取该行,这样您就可以知道该行的名称是男孩还是女孩。然后检查你只需要的名字。总是男孩的名字后面跟女孩的名字,还是可以颠倒?如果(男孩被选中)查看部分[0]或者查看部分[1],那么哈希映射包含{name,popularity}对。这很有意义。我被“钥匙”弄糊涂了。我将在代码中使用它基于他的代码不必创建两个哈希映射,因为前端用户已经选择了性别,因此只需要一个哈希映射。@Justiciar键是用来查找值的,例如,如果哈希映射包含映射到“124797”IntTuple值的“Jacob”键,则“map.get(”Jacob“)”将返回“124797”值,而不是两个if,使用if{}else if{}.No更有意义,因为if{}不满足的每个条件都将执行else语句。因此,如果您的名字与男孩的名字不匹配,它将直接执行else块,即使它与女孩的名字也不匹配,因为您没有在else语句中检查该条件。这看起来可能会让我使用hashmap。尽管如此,我确实有一个运行时错误最初与你。它编译了,但在执行时崩溃了。必须将整数改为双倍。@Islam Hassan:如果第一个条件已经满足,为什么要检查第二个条件?“这是一种不好的做法。”Justiciar说。我试过了,它应该可以很好地处理整数,除非你有非常大的数字(超过20亿)。崩溃时错误消息会说什么?
while ((line = br.readLine()) != null)
{
Scanner sc = new Scanner(line);
int lineNumber = sc.nextInt();
String boyName = sc.next();
int boyNameFreq = sc.nextInt();
String girlName = sc.next();
int girlNameFreq = sc.nextInt();
if(genderSelected.equals("male") && name.equals(boyName)){
// .. a boy's name is found
}
else if(genderSelected.equals("female") && name.equals(girlName)){
// .. a girl's name is found
}
}