I need to create a program that will read from a file and output the information into a JFrame. I know how to create a program to read a file, but I just need some help reading the file to GUI. Any help would be appreciated!

package ism3230_ch6assignment;
import java.io.*;
import java.util.*;

public class ISM3230_Ch6Assignment {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
                                throws FileNotFoundException
    {
/**
 * declaration of variables
 */
        int counter;
        double TestAvg = 0;
        double classAvg = 0;
        String name;
        char grade;
/**
 * input and output file names
 */
        Scanner inFile = new Scanner(new FileReader("612TestScores.txt"));
        PrintWriter outFile = new PrintWriter("612TestScoresout.txt");
/**
 * will print column headers in output file
 */
     outFile.println("Student   Test1 Test2 Test3 Test4 Test5 Average Grade");
/**
 * while loop reads the data from the input file
 */
        while (inFile.hasNext())
        {
/**
 * reads the student's name from the input file and outputs it in the outFile
 */            
            name = inFile.next();

            outFile.printf("%-10s", name);

            TestAvg = calculateAverage(inFile, outFile);

            outFile.printf("%6.2f  ", TestAvg);

            classAvg = classAvg + TestAvg;

            grade = calculateGrade(TestAvg);

            outFile.printf("%3s%n", grade);
        }

        classAvg = classAvg / 10;

        outFile.println();
        outFile.println("Class Average = " + classAvg);

        outFile.close();
   }
/**
 * Void method calculateAverage determines each student's average from the 5
 * test scores and displays the average in the output file.
 */  
   public static double calculateAverage(Scanner inF, PrintWriter outF)
   {
       int score;
       int sum = 0;
       int count;;

       for (count = 1; count <= 5; count++)
       {
           score = inF.nextInt();
           outF.printf("%4d  ", score);
           sum = sum + score;
       }

       return sum / 5.0;
   }
/**
 * Value-returning method calculateGrade determines student's grade from input 
 * data and returns each student's grade in the output file.
 */
   public static char calculateGrade(double avg)
   {
       if (avg >= 90)
           return 'A';
       else if (avg >= 80)
           return 'B';
       else if (avg >= 70)
           return 'C';
       else if (avg >= 60)
           return 'D';
       else
           return 'F';
   }
    }

Recommended Answers

All 4 Replies

How do you want to display the file's content? One easy way would be to put the lines from the file into a JTextArea.

I don't see any GUI code in your post. You have a lot to read about then:
Click Here

I want to put the lines into a JTextArea, but I don't know how to read from the file to the JTextArea

JTextArea has a setText() method and a getText() method use the getText() to get your previous written text and then use setText() to add the new text and the old text. NB this wont work

textarea.setText(whatever+textarea.getText());

you will have to use a temp variable to hold the results of getText()

The append() method would be easier to use than get and set

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.