-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathReadingFromFiles.java
More file actions
36 lines (30 loc) · 910 Bytes
/
ReadingFromFiles.java
File metadata and controls
36 lines (30 loc) · 910 Bytes
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
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ReadingFromFiles
{
public static void main(String[] args) throws IOException
{
int sum = 0;
int count = 0;
double average;
File file = new File("OutputFile.txt");
if (file.exists())
{
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
sum = sum + inputFile.nextInt();
count++;
}
inputFile.close();
average = sum / (double)count;
JOptionPane.showMessageDialog(null,"Sum of all values: " + sum + "\n" + "The number of values: " + count + "\n" + "The average is: " + average);
}
else
{
JOptionPane.showMessageDialog(null,"The file does" + "not exists!");
}
}
}