简体   繁体   中英

Java - Putting words from .txt file into HashMap?

As the title states, I am attempting to read a simple text file and submitting the individual words into a hash map. I will eventually be constructing my program to count the frequency each word, which HashMaps I have the following text file (text.txt):

it was the best of times 
it was the worst of times

it was the age of wisdom 
it was the age of foolishness

it was the epoch of belief 
it was the epoch of incredulity

it was the season of light 
it was the season of darkness

it was the spring of hope 
it was the winter of despair
see the test
try this one

I have written the following c

import java.util.*; 
import java.io.*; 

public class Profile{

  public static String file;
  public static int len;
  public static int count = 0;
  public static String[] words;
  public static String[] unrepeatedWords;

  public static Map<String, Integer> record = new HashMap<String, Integer>();
  //Integer count = record.get(word);
  //Integer count = record.get(word);
  Set<String> keySet = record.keySet(); 



//Method to read whole file
  static void wholeFile(File file){
    try {
            Scanner in = new Scanner(file);
            int lineNumber = 1;

            while(in.hasNextLine()){



              String line = in.nextLine();
              //count += new StringTokenizer(line, " ,").countTokens();
              //System.out.println(line);
              words = line.split("/t");
              words = line.split(" ");
              //System.out.println(words + "");
              lineNumber++;
            }
           for(String word : words){
             //System.out.println(word);
             if(!record.containsKey(word)){ record.put(word, 1); }
             if(record.containsKey(word)){ record.put(word, record.get(word) + 1); }
           }
           System.out.println(record);
           in.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

  }

  Profile(String file){
    this.file = file;
  }
  Profile(String file, int len){
    this.file = file;
    this.len = len;
  }
  public static void main(String[] args){
      file = args[0] + "";
      File a = new File(file);
      //Scanner in = new Scanner(a);

      wholeFile(a);  
  }
}

However, when I run the command run Profile text.txt , I am only storing the last line into the HashMap:

> run Profile text.txt
{one=2, this=2, try=2}
> 

What am I doing incorrectly? How do I efficiently store all words inside of a .txt file inside of a HashMap? Any advice will be helpful.

As other answers have stated, you missplaced your for that handles the split . It should be inside the while , like so:

while (in.hasNextLine()) {
    String line = in.nextLine();
    words = line.split(" ");

    //here so it can use the split from the previous line
    for (String word : words) {
        if (!record.containsKey(word)) {
            record.put(word, 1);
        }
        else {
            record.put(word, record.get(word) + 1);
        }
    }
}

Note that you were also doing two consecutive splits which doesn't make any sense.

You should consider storing your data as a .json file, format it to the standard json format. then parse your data

You need to put the for loop that is putting the words into the hash map inside the while loop. As it is you loop over all lines and then process the last.

Wow, you're making this complicated.

  1. Investigate the Java String split method.

  2. Think about your hash map. For counting, you only want one entry for each unique word. So in pseudocode, you want something like:

    open file for each line in file do for each word in line do if not map.containsKey(word) map.put(word, 1) else -- increment your count here fi od od do something with the results

Suddenly SO won't format that as code.

这是屏幕截图:

Updated to use String.split. Damn whippersnappers.

put for(String word : words) loop inside while (in.hasNextLine()) loop

instead of split(" ") better to use split("\\\\s+") because its free text format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM