Skip to content

Instantly share code, notes, and snippets.

@edolopez
Created April 7, 2014 13:29
Show Gist options
  • Save edolopez/10020326 to your computer and use it in GitHub Desktop.
Save edolopez/10020326 to your computer and use it in GitHub Desktop.
An example of Reading a File with the Scanner Class
4
100 100 100 100
30 45 69 300
400 100 20 40
50 30 70 90
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