-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInput.java
More file actions
57 lines (50 loc) · 1.84 KB
/
Copy pathFileInput.java
File metadata and controls
57 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*import java.io.*;
public class FileInput {
public static void main(String []arg) throws IOException
{
try
{
FileReader reader = new FileReader("data.txt");
BufferedReader in = new BufferedReader(reader);
String name = null; float score, total = 0;
while ((name = in.readLine())!=null){
score = Float.parseFloat(in.readLine());
total += score;
System.out.println("Name:"+ name + "Score:" + score);
}
System.out.println("Total score : " + total);
in.close();
}
catch(FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
}
catch(EOFException eof) {
System.out.println(eof.getMessage());
}
catch(IOException io) {
System.out.println(io.getMessage());
}
}
}*/
import java.io.*;
import java.util.*;
public class FileInput {
public static void main(String []arg) throws IOException {
try {
File reader = new File("data.txt");
Scanner in = new Scanner(reader);
String name = null; float score, total = 0;
while (in.hasNextLine()) {
name = in.next();
score = in.nextFloat();
total += score;
System.out.println("Name: " + name + " Score: " + score);
}
System.out.println("\nTotal score : " + total);
in.close();
}
catch(IOException io) {
System.out.println(io.getMessage());
}
}
}