Source of StudentMarks1.java


  1: //StudentMarks1.java
  2: //Loads student names and marks from a textfile called "input.txt" and
  3: //displays them in a JTextArea component on the screen.

  5: import java.io.File;
  6: import java.io.FileNotFoundException;
  7: import java.util.Scanner;
  8: import javax.swing.JFrame;
  9: import javax.swing.JTextArea;

 11: public class StudentMarks1 extends JFrame
 12: {
 13:     private JTextArea display;

 15:     public StudentMarks1(String title)
 16:     {
 17:         super(title);
 18:         display = new JTextArea(15, 30);
 19:         getDataFromFile();
 20:         add(display);
 21:     }

 23:     public void getDataFromFile()
 24:     {
 25:         try
 26:         {
 27:             Scanner in = new Scanner(new File("input.txt"));
 28:             String line;
 29:             while (in.hasNext())
 30:             {
 31:                 line = in.nextLine();
 32:                 display.append(line + "\n");
 33:             }
 34:             in.close();
 35:         }
 36:         catch (FileNotFoundException e)
 37:         {
 38:             //This exception may or may not happen. It will not happen
 39:             //if a file called "input.txt" is available for reading.
 40:             System.out.println("Error: Required input file was not found.");
 41:             System.exit(0);
 42:         }
 43:     }

 45:     public static void main(String[] args)
 46:     {
 47:         StudentMarks1 app = new StudentMarks1("Record of Student Marks");
 48:         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 49:         app.pack();
 50:         app.setVisible(true);
 51:     }
 52: }