Using Scanner to read words from text file

Java, Tip 20.11.2014 1 Comment

Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example.

Scanner input = new Scanner(file);  
while(input.hasNext()) {
	String word =;

You can find an example of the use of the Scanner class in one our Mortgage Calculator examples.

One Response to “Using Scanner to read words from text file”

  1. […] First step is to read the file and for this we can use the Scanner class to read each number from a text file. […]

Leave a Reply