0

I just got stuck with this BufferedReader and I can't make it to read the whole txt file..it reads only the first line!

FileReader fr = new FileReader("/Users/esson/Desktop/sonnets/sonnet3.txt");
BufferedReader br = new BufferedReader(fr);
String input = br.readLine();
List<String> output= (List) Arrays.asList(input.split(" "));

for(String word: output) {
    int times = Collections.frequency(output, word);
    System.out.println("" + word+ " -- "+times);

and the output is:

When -- 1
most -- 1
I -- 1
wink -- 1
then -- 1
do -- 1
mine -- 1
eyes -- 1
best -- 1
see, -- 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Bogdan Rogin
  • 1
  • 1
  • 1

2 Answers2

2

You need to put BufferedReader.readLine() in a loop. For example:

while((text = BufferedReader.readLine()) != null)

Also, I think you should tag the question as Java and not Javascript

Amoolya S Kumar
  • 1,458
  • 9
  • 10
1
int lineNum;

for(String word: output) {

     lineNum++;

     int times = Collections.frequency(output, word);
     System.out.println("" + word+ " -- "+times);
 }

 System.out.println("Line Number is " + lineNum);
Ahmet
  • 7,527
  • 3
  • 23
  • 47