{"id":339,"date":"2014-11-20T16:28:40","date_gmt":"2014-11-20T05:28:40","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=339"},"modified":"2015-11-20T18:37:17","modified_gmt":"2015-11-20T07:37:17","slug":"scanner-read-words-text-file","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/scanner-read-words-text-file\/","title":{"rendered":"Using Scanner to read words from text file"},"content":{"rendered":"

Often you need to read a file line by line<\/a>. 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.<\/p>\n

\r\nScanner input = new Scanner(file);  \r\nwhile(input.hasNext()) {\r\n\tString word = input.next();\r\n}\r\n<\/pre>\n

You can find an example of the use of the Scanner class in one our Mortgage Calculator examples<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

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. You can find an example of the use of the…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4,89],"tags":[76,97,65,11,99,98],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-5t","jetpack-related-posts":[{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":339,"position":0},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":339,"position":1},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":375,"url":"https:\/\/learn-java-by-example.com\/java\/prompt-user-console-input-validate-response\/","url_meta":{"origin":339,"position":2},"title":"Prompt user for console input and validate their response","date":"December 6, 2015","format":false,"excerpt":"We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":182,"url":"https:\/\/learn-java-by-example.com\/java\/high-guessing-game\/","url_meta":{"origin":339,"position":3},"title":"High Low Guessing Game","date":"May 17, 2011","format":false,"excerpt":"The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":339,"position":4},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":175,"url":"https:\/\/learn-java-by-example.com\/java\/money-change-breakdown\/","url_meta":{"origin":339,"position":5},"title":"Money Change Breakdown","date":"March 28, 2011","format":false,"excerpt":"What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/339"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=339"}],"version-history":[{"count":3,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/339\/revisions\/353"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}