Reputation: 33
This program is attempting to separate a text file into words and then count each time each word is used. The scanner seems to be only reading parts of each line, and I have no idea why. This is my first time using this scanning method.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class WordStats {
public static void main(String args[]){
ArrayList<String> words = new ArrayList<>(1);
ArrayList<Integer> num = new ArrayList<>(1);
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("source.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
boolean set=false;
while (s2.hasNext()) {
num.add(1);
String s = s2.next().replaceAll("[^A-Za-z ]", " ").toLowerCase().trim();
for(int i=0;i<words.size(); i++){
if(s.equals(words.get(i))){
num.set(i,num.get(i)+1);
set=true;
}
}
if(!set){
words.add(s);
num.add(1);
}
}
}
for(int i=0;i<words.size();i++){
System.out.println(words.get(i)+" "+num.get(i));
}
}
}
the text file is the Gettysburg Address:
ABRAHAM LINCOLN, “GETTYSBURG ADDRESS” (19 NOVEMBER 1863)
Fourscore and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate-we consecrate-we can not hallow-this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us-that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion-that we here highly resolve that these dead shall not have died in vain-that this nation, under God, shall have a new birth of freedom-and that government of the people, by the people, for the people shall not perish from the earth.
the original line breaks are preserved. my output seems to only count only part of each line, and also counts whitespace as a word twice. output:
abraham 1
lincoln 1
gettysburg 1
address 1
2
november 1
fourscore 1
and 5
seven 1
years 1
ago 1
our 2
fathers 1
brought 1
forth 1
on 2
this 3
continent 1
a 7
new 2
nation 5
conceived 2
in 4
liberty 1
now 1
we 8
are 2
engaged 1
but 2
It could be something other than the scanning method, but I'm more familiar with that part of the code and I don't think that is it.
Upvotes: 3
Views: 501
Reputation: 1356
You need to reset your boolean set at the beginning of this while loop
while (s2.hasNext()) {
set = false;
once you encounter your first repeated word in each line, set is always true and no new words are being added to your list.
And the whitespace count is because of how your replaceall handles the "(19" and "1863)" since there are no alphabetical characters in those "words".
Upvotes: 1
Reputation: 109613
The logic is a bit skewed. You have parallel lists that should have the same number of elements, but are not added in parallel.
Map<String, Integer> wordFrequencies = new TreeMap<>();
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String word = s2.next().replaceAll("[^A-Za-z ]", " ")
.toLowerCase().trim();
Integer n = wordFrequencies.get(word);
wordFrequencies.put(word, n == null ? 1 : 1 + n);
}
}
for (Map.Entry<String, Integer> entry : wordFrequencies.entrySet()) {
System.out.printf("%-40s %5d%n", entry.getKey(), entry.getValue());
}
Upvotes: 1
Reputation: 727097
The problem is that your code unconditionally adds 1
to the num
list on each iteration of the loop. This shifts num
s in relation to words
, producing wrong output.
Removing num.add(1);
from the nested while
loop would fix the problem. However, a better approach would be making a Map<String,Integer>
to track counts. In addition to ensuring that counts and words would always be in a synchronized state, this change would let you remove the nested while
loop altogether, and use fast lookup based on your map's algorithm.
Upvotes: 1