Start by highlighting lines 9 to 159 inclusive. Push the key with “del” written on.
Now you can actually create your application.
Get rid of that long main method. I think a main method has an ideal length . . . of one line.
Do it in stages.
Start by trying to read words. Create yourself a FileScanner class which has a java.io.File object as a field, and uses that File object to initialise a Scanner (or other means for reading). Give it one (
public) constructor which takes the File object as a parameter. Give it methods like nextToken, nextInt, nextBigInteger, hasNextInt, hasNextToken, nextLine, etc. Comparison with the java.util.Scanner class will show that you are creating a “wrapper” round a Scanner object. all the methods I have suggested have counterparts with similar names in the Scanner class. You may suffer a FileNotFoundException in the constructor; that should be re-thrown not caught, so you have to use the
throws keyword. You can read about re-throwing Exceptions
here. You can also get the most recent Exception; there is a method in the Scanner class which returns that Exception.
Now you can test it like this:
Note the way I have used
args means, in my version, you must invoke it like this
You must supply the name of the file, and the "" are to make sure the runtime doesn’ separate it into two on the space.
Now you have got that bit working, you can forget the FileReadingDemo class and work out how to put the individual words together. But that is for later. Don’t try to do it all at once. Get one part working and only then consider the next part.
You can read more about file reading
here; the section about scanning tells you about Scanners. There is an example similar to what you want.