0

I'm writing a program that reads in a text file line by line - then stores each sentence in a HashMap with the sentence as the key and number of letters in that sentence as the object. This is the code I wrote, with comments:

    FileReader reader = new FileReader(new File("src/test.txt"));
    BufferedReader br = new BufferedReader(reader);
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    StringBuilder builder = new StringBuilder();
    String line, sentence;
    int letters = 0;
    try {
        // While there are more lines to read 
        while ((line = br.readLine()) != null) {
            String[] words = line.split(" "); // split line into words, add to array
            for (int i = 0; i < words.length; i++) { // loop through array
                letters += words[i].length(); 
                // if the word ends with "." then we have reached end of sentence
                if (Character.toString(words[i].charAt(words[i].length() - 1)) == ".") { 
                    for(String word : words) {
                        if(builder.length() > 0) {
                            builder.append(" ");
                        }
                        builder.append(word);
                    }
                    sentence = builder.toString(); // store sentence
                    map.put(sentence, letters); // add to HashMap
                    letters = 0; // restore letters back to 0
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    for(String key : map.keySet()) {
        System.out.println(key + " has " + map.get(key) + " letters");
    }

For some reason in that little for-loop at the end nothing gets printed. I notice that if I write a print statement before the for-loop to print out the size of my HashMap it prints out "0" - so I'm assuming that my HashMap isn't being populated.

Does anyone know why this might be the case? This is the text file I'm using:

this is a test. 
this is a test. 
this is not a test. 
this is a test. 
this is not a test. 
not not not not not not not

Really appreciate any insight, cheers.

eyes enberg
  • 556
  • 1
  • 8
  • 30
  • Try calling an output to the console of the map. – liquidsystem Dec 24 '15 at 01:41
  • 3
    You need to use `.equals` to compare strings, not `==`. Basically the if statement never sees a true condition. Although I agree the question was marked duplicate prematurely. – bcc32 Dec 24 '15 at 01:44
  • 1
    You didn't debug this code first, find out where the error was coming from, something that's usually a good idea before coming here. At least use println's to see where the misbehavior originates. If you would have done this, you'd find that the if block wasn't working as expected, and this would have focused your attention on it. – Hovercraft Full Of Eels Dec 24 '15 at 01:44
  • Ah yeah, thanks for that. I noticed that the duplicate link was actually provided at the top and it fixed my problem. It was indeed the .equals vs == situation lol. Apologies. – eyes enberg Dec 24 '15 at 01:45
  • @bcc32: The solution is in the duplicate thread. So closing it is justified. – sstan Dec 24 '15 at 01:45
  • @sstan I agree but it was marked duplicate before it was explained to the asker that that was even the cause. Seems a bit hasty to me. – bcc32 Dec 24 '15 at 01:46
  • @bcc32: better than having a duplicate answered unnecessarily. Better to close, and let the question die and be deleted. Adding yet another String `==` vs `equals` will add no value to this site, nothing that will help future users. – Hovercraft Full Of Eels Dec 24 '15 at 01:46
  • 1
    @bcc32: Actually, if anything, OP was a bit too hasty in asking the question. – sstan Dec 24 '15 at 01:47

0 Answers0