Jump to content

So im having issues with a simple-ish program im writing for class and ive got everything done with this part of the project however ive run into a ditch. I cant figure out why my code will print and ask for input twice for the 2nd, 3rd, and 4th scan.nextLine() and scan.nextByte() parts when it is assigning course number and course credits to variables. Ill comment next to the part of the code im talking about. Anyways i just need it to ask for them 1 at a time so it can assign them instead of asking for both of them and then i can finish my code. Honestly dont worry about any of the code past the part that im commenting next to I dont think it will really matter to you what that is. And yes i realize this code may not be very efficient and I probably use outdated methods and whatnot, teacher restricts us to certain content we can use for now.

 

 

 

 

 

import java.util.Scanner;

public class Part1 {
    public static void main (String []args)
    {
        Scanner scan=new Scanner(System.in);
        
        System.out.println("Hello, Welcome to the GPA calculator");
        
        String firstName, lastName, majorDegree, courseNum1, courseNum2, courseNum3, courseNum4;
        byte courseCredits1, courseCredits2, courseCredits3, courseCredits4;
        String qPoints1, qPoints2, qPoints3, qPoints4;
        double calc1_1, calc1_2, calc1_3, calc1_4;
        
        
        
        System.out.println("Please enter your first name.");
        firstName=scan.nextLine();
        
        System.out.println("Please enter your last name.");
        lastName=scan.nextLine();

        System.out.println("What is your Major?");
        majorDegree=scan.nextLine();
        
        System.out.println("Please enter the course number for your first class.");
        courseNum1=scan.nextLine();
        
        System.out.println("How many credits was this first course worth?");
        courseCredits1=scan.nextByte();
        
        {
            System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
        }
        
        System.out.println("Please enter exactly how many Quality points you received for your first class based on the chart above.");
        qPoints1=scan.next();
        String numConvert1= qPoints1;
        float finalNum1 = Float.parseFloat(numConvert1);
        calc1_1= finalNum1*courseCredits1;
        
        
        
        System.out.println("Please enter the course number for your second class."); //This is where im having trouble.....................................................................................................................................................................
        courseNum2=scan.nextLine();
        
        
        System.out.println("How many credits was this second course worth?"); //This is also where im having trouble......................................................................................................................................................................
        courseCredits2=scan.nextByte();
        
        System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
        
        System.out.println("Please enter exactly how many Quality points you received for your second class based on the chart above.");
        qPoints2=scan.next();
        String numConvert2= qPoints2;
        float finalNum2 = Float.parseFloat(numConvert2);
        calc1_2= finalNum2*courseCredits2;
            
        
        System.out.println("Please enter the course number for your third class.");
        courseNum3= scan.nextLine();
        
        System.out.println("How many credits was this third course worth?");
        courseCredits3= scan.nextByte();
        
        {
            System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
        }
        
        System.out.println("Please enter exactly how many Quality points you received for your third class based on the chart above.");
        qPoints3=scan.next();
        String numConvert3= qPoints3;
        float finalNum3 = Float.parseFloat(numConvert3);
        calc1_3= finalNum3*courseCredits3;
        
        
        System.out.println("Please enter the course number for your fourth class.");
        courseNum4= scan.nextLine();
        
        System.out.println("How many credits was this fourth course worth?");
        courseCredits4= scan.nextByte();
        
        {
            System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
        }
        
        System.out.println("Please enter exactly how many Quality points you received for your fourth class based on the chart above.");
        qPoints4=scan.next();
        String numConvert4= qPoints4;
        float finalNum4 = Float.parseFloat(numConvert4);
        calc1_4= finalNum4*courseCredits4;
        
        String totalQualityPoints= qPoints1+qPoints2+qPoints3+qPoints4;
        int finalTotalQPoints= Integer.parseInt(totalQualityPoints);
        
        int totalCourseCredits= courseCredits1+courseCredits2+courseCredits3+courseCredits4;
        
        int gpa= finalTotalQPoints/totalCourseCredits;
        
    }
}

Link to comment
https://linustechtips.com/topic/850381-issue-with-simple-java-gpa-calculator/
Share on other sites

Link to post
Share on other sites

I ran this code on my local system and it seems that you are attempting to parse an invalid string on line 97 as it is a mesh of all the strings that where entered above and then trying to cast it to an integer. what are you attempting to do?

Link to post
Share on other sites

12 minutes ago, Pcgeekbrain said:

I ran this code on my local system and it seems that you are attempting to parse an invalid string on line 97 as it is a mesh of all the strings that where entered above and then trying to cast it to an integer. what are you attempting to do?

So idk if that portion of the code is part of my problem cause it seems to work fine and also when i comment it out i still get my main problem from above. However i was trying to add all of the "Quality Points" together but since theyre in string form i added them all together in string form and then converted them from string to an int value. I think i need to change it to convert it from string to float or double later but i think for the most part ill be able to get that to work i just need to figure out why its asking for course number and course credits input at the same time.

Link to post
Share on other sites

I have made some changes to the code, mostly just making your scanner instance call the next function instead of the nextLine function. It seems to not skip over the input now as it was before. I would highly suggest you modularise your code into functions and run them as you have a bunch of code that basically does the same thing. However, if you guys haven't gotten into data structures and loops and functions yet I wouldn't fret. This is the functional code, however, I seem to be getting a GPA of 200+ which sounds great if it was true, but in my experience GPA doesn't range past single digits. 

 

import java.util.Scanner;

public class GPA {
        public static void main (String []args)
        {
            Scanner scan=new Scanner(System.in);

            System.out.println("Hello, Welcome to the GPA calculator");

            String firstName, lastName, majorDegree, courseNum1, courseNum2, courseNum3, courseNum4;
            byte courseCredits1, courseCredits2, courseCredits3, courseCredits4;
            String qPoints1, qPoints2, qPoints3, qPoints4;
            double calc1_1, calc1_2, calc1_3, calc1_4;



            System.out.println("Please enter your first name.");
            firstName=scan.nextLine();

            System.out.println("Please enter your last name.");
            lastName=scan.nextLine();
            System.out.println("What is your Major?");
            majorDegree=scan.nextLine();

            System.out.println("Please enter the course number for your first class.");
            courseNum1=scan.next();

            System.out.println("How many credits was this first course worth?");
            courseCredits1=scan.nextByte();

            {
                System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
            }

            System.out.println("Please enter exactly how many Quality points you received for your first class based on the chart above.");
            qPoints1=scan.next();
            String numConvert1= qPoints1;
            float finalNum1 = Float.parseFloat(numConvert1);
            calc1_1= finalNum1*courseCredits1;



            System.out.println("Please enter the course number for your second class."); //This is where im having trouble.....................................................................................................................................................................
            courseNum2=scan.next();


            System.out.println("How many credits was this second course worth?"); //This is also where im having trouble......................................................................................................................................................................
            courseCredits2=scan.nextByte();

            System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");

            System.out.println("Please enter exactly how many Quality points you received for your second class based on the chart above.");
            qPoints2=scan.next();
            String numConvert2= qPoints2;
            float finalNum2 = Float.parseFloat(numConvert2);
            calc1_2= finalNum2*courseCredits2;


            System.out.println("Please enter the course number for your third class.");
            courseNum3= scan.next();

            System.out.println("How many credits was this third course worth?");
            courseCredits3= scan.nextByte();

            {
                System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
            }

            System.out.println("Please enter exactly how many Quality points you received for your third class based on the chart above.");
            qPoints3=scan.next();
            String numConvert3= qPoints3;
            float finalNum3 = Float.parseFloat(numConvert3);
            calc1_3= finalNum3*courseCredits3;


            System.out.println("Please enter the course number for your fourth class.");
            courseNum4= scan.next();

            System.out.println("How many credits was this fourth course worth?");
            courseCredits4= scan.nextByte();

            {
                System.out.println(" QUALITY POINTS:\n _________________________ \n A 4.00 A- 3.67 \n B+ 3.33 B 3.00 B- 2.67 \n C+ 2.33 C 2.00 C- 1.67 \n D+ 1.33 D 1.00 \n F 0.00");
            }

            System.out.println("Please enter exactly how many Quality points you received for your fourth class based on the chart above.");
            qPoints4=scan.next();
            String numConvert4= qPoints4;
            float finalNum4 = Float.parseFloat(numConvert4);
            calc1_4= finalNum4*courseCredits4;

            String totalQualityPoints= qPoints1+qPoints2+qPoints3+qPoints4;
            int finalTotalQPoints= Integer.parseInt(totalQualityPoints);

            int totalCourseCredits= courseCredits1+courseCredits2+courseCredits3+courseCredits4;

            int gpa= finalTotalQPoints/totalCourseCredits;

            System.out.println("Your GPA is : "+gpa);

        }

}

Slightly off topic, but next time you post some code to the forums, using the small code button is awesome as it lays out your code in a nice box. 

 

EDIT : 

 

If you would like to see how I would write the program if I were you here's a link to my version of your code. In writing this program, assuming this is an initial stage and you will be using course numbers, the person's name and major later (as it's redundant asking for them if they are not going to be used) I would use a Course datatype that stores all of your important values and would also use a for loop when asking for details as it's the same text over and over again. My implementation also takes students who either underload ( taking less than 4 courses)  or overload ( taking more than 4 courses) into consideration. 

Link to post
Share on other sites

Oh ok. And also i super appreciate the code it helped me understand what I should be doing but the only problem is that since im only a couple units through a basic java class were not aloud to use things like arrays, if/else/for statements, loops, or even multiple classses(at least for this project). He wants it all in 1 class and were not aloud to use those aforementioned things but believe me if i were aloud to use multiple classes and some if/else statements I would've been done with this project after a couple hours.

Link to post
Share on other sites

1 hour ago, Mehoy Minoy said:

Oh ok. And also i super appreciate the code it helped me understand what I should be doing but the only problem is that since im only a couple units through a basic java class were not aloud to use things like arrays, if/else/for statements, loops, or even multiple classses(at least for this project). He wants it all in 1 class and were not aloud to use those aforementioned things but believe me if i were aloud to use multiple classes and some if/else statements I would've been done with this project after a couple hours.

That's a shame with your requirements. But it makes sense why you wrote it the way you did. Best of luck.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×