簡體   English   中英

將.txt文件讀入Hashmap並進行操作

[英]Reading .txt File into a Hashmap and manipulating it

盡管在看了一些教程之后,我最大的問題很可能是沒有完全了解Hashmaps以及如何操作它們。 希望您明智的靈魂能夠指出我正確的方向。

我正在嘗試將.txt文件讀入哈希圖中。 文本文件包含2006年流行的名稱。inputFile的每一行包含一個男孩名和一個女孩名,以及被命名的名字。 例如:1 Jacob 24,797 Emily 21,365將是文件中第1行的輸入。

我想將男孩名字放在一個列表中,並將女孩名字放在第二個列表中,以保持其當前位置,以便用戶可以搜索jacob並被告知那是那一年的第一個男孩名字,以此類推。 。 以前,我只是逐行讀取文件,然后查看文件包含我要搜索的名稱的行。 這行得通,但是無法分辨是男孩還是女孩,這會導致錯誤,如果我說我正在搜索雅各布在女孩中的流行程度,它仍然會說數字1。我確定哈希表會是解決此問題的最佳方法,但卻無法真正發揮作用。

我的密碼

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);
}

樣本inputFile:

1   Jacob   24,797  Emily   21,365
2   Michael 22,592  Emma    19,092
3   Joshua  22,269  Madison     18,599
4   Ethan   20,485  Isabella    18,200
5   Matthew 20,285  Ava     16,925
6   Daniel  20,017  Abigail     15,615
7   Andrew  19,686  Olivia  15,474
8   Christopher 19,635  Hannah  14,515

您將需要兩個哈希圖,一個用於男孩的名字,一個用於女孩的名字-目前,您正在使用男孩的名字作為鍵,而使用女孩的名字作為值,這不是您想要的。 而是使用兩個Map<String, IntTuple>數據結構,其中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
    }

}

掃描程序類用於解析該行,並逐個令牌讀取它,因此您可以知道該名稱是男孩還是女孩。 然后檢查僅需要的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM