{"id":205,"date":"2011-09-12T10:11:49","date_gmt":"2011-09-12T00:11:49","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=205"},"modified":"2015-11-20T16:58:40","modified_gmt":"2015-11-20T05:58:40","slug":"counting-numbers","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","title":{"rendered":"Counting numbers"},"content":{"rendered":"

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.<\/p>\n

First step is to read the file and for this we can use the Scanner class to read each number from a text file<\/a>.<\/p>\n

As we read each number we need to keep track of how many numbers we have read and a running total of the numbers read. Also need to check if each number is a new minimum or maximum.<\/p>\n

Once we have read all the numbers from the file we can calculate the average value (we now know the total of all numbers read and how many numbers there were).<\/p>\n

Finally we can output the results to the console. And we’re done.<\/p>\n

Have a read through the code and try it out for yourself. As always, let us know if you have any questions.<\/p>\n

\r\npackage com.learnjava.math;\r\n\r\nimport java.io.File;\r\nimport java.io.FileNotFoundException;\r\nimport java.util.Scanner;\r\n\r\npublic class Numbers {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\ttry {\r\n\t\t\tFile file = new File("numbers.txt");\r\n\t\t\tScanner input = new Scanner(file);\r\n\t\t\t\r\n\t\t\t\/\/ Count how many numbers\r\n\t\t\t\r\n\t\t\tint count = 0;   \/\/ Count of numbers read\r\n\t\t\t\r\n\t\t\t\/\/ Sum of all numbers\r\n\t\t\t\r\n\t\t\tint total = 0;\r\n\t\t\t\r\n\t\t\t\/\/ Smallest number\r\n\r\n\t\t\tint min = Integer.MAX_VALUE;\r\n\t\t\t\r\n\t\t\t\/\/ Largest number\r\n\t\t\t\r\n\t\t\tint max = Integer.MIN_VALUE;\r\n\t\t\t\r\n\t\t\t\/\/ Loop until no more numbers to be read from file\r\n\t\t\t\r\n\t\t\twhile (input.hasNext()) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Read next number from file\r\n\t\t\t\t\r\n\t\t\t\tint next = input.nextInt();\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Increnment count\r\n\t\t\t\t\r\n\t\t\t\tcount++;\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Add number to total\r\n\t\t\t\t\r\n\t\t\t\ttotal += next;\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Update minium\r\n\t\t\t\t\r\n\t\t\t\tmin = Math.min(min,  next);\r\n\t\t\t\t\r\n\t\t\t\t\/\/ Update maximum\r\n\t\t\t\t\r\n\t\t\t\tmax = Math.max(max, next);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tinput.close();\r\n\t\t\t\r\n\t\t\t\/\/ All numbers have been read\r\n\t\t\t\/\/ Can now calculate the average\r\n\t\t\t\/\/ NB. This uses integer arithmetic so result will be rounded\r\n\t\t\t\r\n\t\t\tint average = total \/ count;\r\n\t\t\t\r\n\t\t\t\/\/ Finally display results to console\r\n\t\t\t\r\n\t\t\tSystem.out.println(count+" numbers");\r\n\t\t\tSystem.out.println("Sum of numbers "+total);\r\n\t\t\tSystem.out.println("Smallest number "+min);\r\n\t\t\tSystem.out.println("Largest number "+max);\r\n\t\t\tSystem.out.println("Average of all numbers "+average);\r\n\t\t\t\r\n\t\t} catch (FileNotFoundException ex) {\r\n\t\t\t\r\n\t\t\t\/\/ We'll end up here if the file numbers.txt cannot be found\r\n\t\t\t\r\n\t\t\tSystem.out.println("Could not find the output file");\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

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 for this we can use…<\/p>\n","protected":false},"author":1,"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":[24,4],"tags":[56,51,54,55,52,53,33,57],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-3j","jetpack-related-posts":[{"id":335,"url":"https:\/\/learn-java-by-example.com\/java\/read-text-file-line-line\/","url_meta":{"origin":205,"position":0},"title":"How do I read a text file line by line?","date":"November 20, 2013","format":false,"excerpt":"The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":339,"url":"https:\/\/learn-java-by-example.com\/java\/scanner-read-words-text-file\/","url_meta":{"origin":205,"position":1},"title":"Using Scanner to read words from text file","date":"November 20, 2014","format":false,"excerpt":"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\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":238,"url":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","url_meta":{"origin":205,"position":2},"title":"Selection Sort","date":"June 19, 2015","format":false,"excerpt":"When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It's not a particularly efficient algorithm and really only suitable for\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":154,"url":"https:\/\/learn-java-by-example.com\/java\/prime-numbers\/","url_meta":{"origin":205,"position":3},"title":"Prime numbers","date":"January 21, 2011","format":false,"excerpt":"The assignment here is to calculate all prime numbers less than 100. The solution provided uses the following to determine if a given number is prime. 2 is prime Any number divisible by 2 is not prime If the number is divisible by any odd number then it is not\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":205,"position":4},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":253,"url":"https:\/\/learn-java-by-example.com\/java\/copy-file-java-6\/","url_meta":{"origin":205,"position":5},"title":"How to copy a file using Java 6","date":"July 10, 2015","format":false,"excerpt":"Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.","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\/205"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":5,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/205\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}