0

I have created a hashmap> called myMap and i want to store all unique words in the keys of Hashmap and names of the files in values corresponding to key words. taking input from multiple files and storing their names in values corresponding to key words.

Sample input from 1 file

AS YOU LIKE IT

by William Shakespeare



DRAMATIS PERSONAE.

DUKE, living in exile
FREDERICK, his brother, and usurper of his dominions
AMIENS, lord attending on the banished Duke
JAQUES,   "      "       "  "     "      "
LE BEAU, a courtier attending upon Frederick
CHARLES, wrestler to Frederick
  OLIVER, son of Sir Rowland de Boys
JAQUES,   "   "  "    "     "  "

Code

String name = f.getName();

    BufferedReader in = new BufferedReader(new FileReader(f));
    String words = in.readLine().trim().toLowerCase();

    for(String word: words.split("\\s+")) {
        if (myMap.get(word) == null) {
            myMap.put(word, new ArrayList<String>());
        }
            myMap.get(word).add(name);
        }

i am able to read only first line from all the files..

Shalitha Suranga
  • 967
  • 7
  • 20
  • you need to call `in.readLine()`in a loop. – StephaneM Dec 12 '17 at 11:57
  • Because you only ever read one line. – domsson Dec 12 '17 at 11:57
  • @StephaneM while(in.readLine() ! = null) running this will stop when it read first blank line.. – Gaurav Rajput Dec 12 '17 at 12:24
  • in.readLine() will return an empty string on blank line, not null. – StephaneM Dec 12 '17 at 12:30
  • @GauravRajput you might want to look through java.util.Map API https://docs.oracle.com/javase/7/docs/api/java/util/Map.html and see methods like *containsKey()* Also see what is difference between *HashSet* and *HashMap* -- you might find Sets more suitable for using as values in *myMap* – Lauri Dec 12 '17 at 14:40

3 Answers3

1

This is a different approach for reading all lines:

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines(java.nio.file.Path,%20java.nio.charset.Charset)

Ben Sch
  • 81
  • 1
  • 6
  • I would have suggested using LOOP, but this readAllLines from java.nio is even better answer :) – Lauri Dec 12 '17 at 12:03
  • @Lauri a loop is a better **general** answer, because you can use it to process files too large to fit in heap memory. – slim Dec 12 '17 at 12:49
1

I see that you're trying to solve some kind of homework.

There are already some relevant and good answers given to you, but there is another approach, that might be relevant as well -- specially in case you're taking some programming class.

It is called the "googling approach"

Depending on your previous search queries, you may get better or worse results. However on the google.com site you can really fast find relevant results for some common programming problems. For example I search for "reading all lines from file in java" and first result I get is: How to read a large text file line by line using Java?

Similar question to yours but with already 19 answers and best one with over 700 up votes.

To narrow down your search results, for example to only show results from stackoverflow, you may search so: "reading all lines from file texts in java site:stackoverflow.com"

Lauri
  • 1,643
  • 2
  • 12
  • 16
0

This is nothing to do with the blank lines.

You call in.readLine().trim().toLowerCase(); once. It reads one line.

You will need to call it again to get the second line, and again to get the third line, and so on.

I'm sure you already know what a loop is -- use one.

slim
  • 36,139
  • 10
  • 83
  • 117