Created
April 7, 2014 13:29
-
-
Save edolopez/10020326 to your computer and use it in GitHub Desktop.
An example of Reading a File with the Scanner Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 4 | |
| 100 100 100 100 | |
| 30 45 69 300 | |
| 400 100 20 40 | |
| 50 30 70 90 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| import java.util.Scanner; | |
| public class ReadFile { | |
| public static void main(String[] args) throws IOException { | |
| Scanner input = new Scanner(System.in); | |
| Scanner file = new Scanner(new File("info.txt")); | |
| int numLines = file.nextInt(); | |
| int sum = 0; | |
| String textLine = ""; | |
| for (int line = 0; line < numLines; line++) { | |
| for (int data = 0; data < 4; data++) { | |
| sum = sum + file.nextInt(); | |
| } | |
| System.out.println("Mean: " + (double)(sum / 4.0)); | |
| sum = 0; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment