Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- import java.io.*;
- public class ReadData1 {
- public static void main ( String args[])
- {
- int data [ ] = new int [5];
- int num, total = 0;
- BufferedReader pr = null;
- File newFile = new File ("results.txt");
- try {
- pr = new BufferedReader ( new FileReader (newFile));
- String s = new String ( pr.readLine ( )); //read name
- String output = new String(s);
- for( int i = 0; i<4; i++){ //read 4 grades
- s = pr.readLine ();
- num = Integer.parseInt(s); // convert string to num
- data[i] = num; // store number in the array
- total = total + num;
- output = output + " " + num;
- }
- output = output + " Avg = " + (total/ 4.0);
- pr.close();
- JOptionPane.showMessageDialog( null, output, "Read From File",
- JOptionPane.INFORMATION_MESSAGE);
- }
- catch (IOException ex){
- JOptionPane.showMessageDialog( null, "IO error", "Read File failed",
- JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement