GPA Calculation Program using an input file
I am very confused about what to do with this assignment. I am relatively new to java programming and I have to calculate GPA when given a grade and the corresponding amount of credits in an input file. The exact description is
Quote:
The course points equal (gradePoints x credits). Write a program that reads data from a text file that contains a sequence of (letter grade, credits) pairs, and calculates the overall GPA of the student. An example of the input may look something like this:
B- 3
D+ 2
A 3
W 0
C+ 4
B 3
The first requirement is to "1. Use a method that takes a letter grade and a number of credits as parameters, calculates and returns the points for the course."
My problem currently is that I am unsure how to create a new method after the main and still keep the information from the input file. my code so far is below.
Code :
import java.util.Scanner;
import java.io.*;
public class GPA
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Darrien Kamai");
System.out.println("CPS 180");
String filename = scan.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
String gradePoint, readData;
//Read grade and amount credits from data file
public static void readData(String grade, int credits) throws IOException
{
grade = inputFile.nextLine();
credits = inputFile.nextInt();
}
}
Any help would be very appreciated as I am completely stuck with the new method. the program is giving an illegal start of expression error
Re: GPA Calculation Program using an input file
Quote:
an illegal start of expression error
Check the there is a } for every {.
Quote:
keep the information from the input file
Move the code from the main method into a method in the class. Have the main method create an instance of the class and call that method. Put the data into class variables where it will be available to all the methods in the class.
Re: GPA Calculation Program using an input file
Code :
import java.util.Scanner;
import java.io.*;
public class GPA
{
public static void main(String[] args) throws IOException
{
System.out.println("Darrien Kamai");
System.out.println("CPS 180");
//Variables
String filename;
double coursePoints = 0;
//Get the file name
filename = getFileName();
//Get course Points
coursePoints = getCoursePoints();
System.out.println(coursePoints);
}
public static String getFileName()
{
Scanner scan = new Scanner(System.in);
String file;
System.out.println("Enter the name of the file.");
file = scan.nextLine();
return file;
}
public static double getCoursePoints() throws IOException
{
String filename = "";
String grade;
double gradeNum = 0, coursePoints = 0;
int credits = 0, previousCredits = 0, totalCredits = 0;
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
grade = inputFile.nextLine();
System.out.print(grade);
switch (grade) {
case "A": gradeNum = 4.0; break;
case "A-": gradeNum = 3.7; break;
case "B+": gradeNum = 3.3; break;
case "B": gradeNum = 3.0; break;
case "B-": gradeNum = 2.7; break;
case "C+": gradeNum = 2.3; break;
case "C": gradeNum = 2.0; break;
case "C-": gradeNum = 1.7; break;
case "D+": gradeNum = 1.3; break;
case "D": gradeNum = 1.0; break;
case "D-": gradeNum = 0.7; break;
case "E": gradeNum = 0.0; break; }
credits = previousCredits;
credits = inputFile.nextInt();
System.out.println(credits);
totalCredits = credits + previousCredits;
coursePoints = gradeNum * credits;
}
System.out.println("Total Credits: " + totalCredits);
inputFile.close();
return coursePoints;
}
}
Ok so I am at a point where i have simplified my methods and done a bit more. Now my issue is the error
java.io.filenotfoundexception:
(in java.io.FileInputStream)
Not really sure what that means any help is appreciated
Re: GPA Calculation Program using an input file
Quote:
filenotfoundexception:
The message says that the File was Not Found.
Where is the stack trace that shows what line the error occurred on?
You should copy and paste the full text of the error message, not type it in from memory.