Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GPA Calculation Program using an input file

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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.

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: GPA Calculation Program using an input file

    an illegal start of expression error
    Check the there is a } for every {.

    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.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GPA Calculation Program using an input file

    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: GPA Calculation Program using an input file

    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.

Similar Threads

  1. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  2. Shape Calculation Program
    By TH1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 02:54 PM
  3. Program help with counting even and odd integers in input value.
    By fueledbyrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2010, 07:02 PM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM

Tags for this Thread