Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.io.*;
  3.  
  4. public class ReadData1 {
  5.  
  6.   public static void main ( String args[])
  7. {
  8.   int data [ ] = new int [5];
  9.     int num, total = 0;
  10.     BufferedReader pr = null;
  11.     File newFile = new File ("results.txt");
  12.     try {
  13.  
  14.     pr = new BufferedReader ( new FileReader (newFile));
  15.  
  16.     String s = new String ( pr.readLine ( )); //read name
  17.     String output = new String(s);
  18.  
  19.     for( int i = 0; i<4; i++){              //read 4 grades
  20.       s = pr.readLine ();
  21.       num = Integer.parseInt(s);      // convert string to num
  22.       data[i] = num;                  // store number in the array
  23.       total = total + num;
  24.       output = output + " " + num;
  25.     }
  26.     output = output + " Avg = " + (total/ 4.0);
  27.     pr.close();
  28.  
  29.     JOptionPane.showMessageDialog( null, output, "Read From File",
  30.                                      JOptionPane.INFORMATION_MESSAGE);
  31.   }
  32.   catch (IOException ex){
  33.     JOptionPane.showMessageDialog( null, "IO error", "Read File failed",
  34.                                      JOptionPane.INFORMATION_MESSAGE);
  35.     }
  36.  
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement