Summing Several Numbers from Any File
source link: http://programmingbydoing.com/a/summing-several-numbers-from-any-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- Author: Graham Mitchell
- Filename: SummingSeveralNumbersFromAnyFile.java
Summing Several Numbers from Any File
Create a few more files in Notepad containing some integers separated by some whitespace. Save them in your home directory as "4nums.txt", "5nums.txt", "6nums.txt", etc.
Then write a program that asks the user which file to open. Then it should open that file, and read in and total up all the integers from that file.
You won't be able to store each number in its own variable anymore. Instead, you'll need to use a loop, and reuse the same variable over and over. You'll have to add up the numbers as you go. You've done this before.
Which file would you like to read numbers from: 5nums.txt Reading numbers from "5nums.txt" 1 2 3 4 5 Total is 15
Which file would you like to read numbers from: 8nums.txt Reading numbers from "8nums.txt" 3 1 4 1 5 9 2 6 Total is 31
©2013 Graham Mitchell
This assignment is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK