Reputation: 103
I need to open a file test.txt
this file only has one sentence, but it is all in one line only. My job is to separate each word and display all the words that are misspelled.
I've tried using a BufferReader and FileReader, but it just prints out the name of the file. I want it to see the first line and essentially put all the words in an array. If anyone can explain how exactly I should be using BufferReader or FileReader would be great.
This is test.txt:
The warst drought in the United States in neearly a century is expected to drive up the price of milk, beef and pork next yeer, the government said Wednesdaay, as consumers bear some of the bruntt of the sweltering heat that is drivng up the cost of feed corrn.
Note: This appears as one single line in the editor.
This is what I tried:
FileReader fr = new FileReader("test.txt");
BufferReader br = new BufferReader(fr);
StringBuilder sb = new StringBuilder();
String s;
while((s = br.readLine()) != null){
sb.append(s);
sb.toString();
}
Thanks for your help.
Upvotes: 0
Views: 33
Reputation: 1847
for (String line : Files.readAllLines(Paths.get("filepath.txt"))) {
// ...
}
Upvotes: 1