Reputation: 23
I have this, but I'm very lost in how I can get the final GPA to print out, I've tried various ways but have not been able to do it successfully. This is what I have:
Scanner input = new Scanner(System.in);
System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();
String[] gradesArray = new String[length];
for(int i = 0; i < gradesArray.length; i++)
{
System.out.println("Enter grade (include + or -) ");
gradesArray[i] = input.nextLine();
double points = 0.0;
if(gradesArray[i].equalsIgnoreCase("A+") || gradesArray[i].equalsIgnoreCase("A"))
{
points += 4;
}
else if(gradesArray[i].equalsIgnoreCase("A-"))
{
points+= 3.7;
}
else if(gradesArray[i].equalsIgnoreCase("B+"))
{
points += 3.3;
}
else if(gradesArray[i].equalsIgnoreCase("B"))
{
points += 3.0;
}
else if(gradesArray[i].equalsIgnoreCase("B-"))
{
points += 2.7;
}
else if(gradesArray[i].equalsIgnoreCase("C+"))
{
points += 2.3;
}
else if(gradesArray[i].equalsIgnoreCase("C"))
{
points += 2.0;
}
else if(gradesArray[i].equalsIgnoreCase("D"))
{
points += 1.0;
}
else if(gradesArray[i].equalsIgnoreCase("F"))
{
points += 0.0;
}
else
{
System.out.println("Invalid grade");
}
System.out.println("GPA: " + points / gradesArray.length);
}
I'm guessing the GPA does not print out properly because after the condition matches the grade, it then goes right down to print, right? And also, how can I do it so if they enter an invalid grade, it makes the user start over.
Upvotes: 1
Views: 99
Reputation: 1801
Aside from @Alex K's answer. Not sure if you have learned about HashMaps
or not, but you could shorten your code down quite a bit by using them. It also makes it way easier to add another grade and point value with ease.
Scanner input = new Scanner(System. in );
System.out.println("How many grades are you putting? ");
int length = input.nextInt();
String[] gradesArray = new String[length];
//Create a HashMap with String as the key, and a Double as the value
Map < String, Double > grades = new HashMap < > ();
//Insert all the grades and point values
grades.put("A+", 4.0);
grades.put("A-", 3.7);
grades.put("B+", 3.3);
grades.put("B", 3.0);
grades.put("B-", 2.7);
grades.put("C+", 2.3);
grades.put("C", 2.0);
grades.put("D", 1.0);
grades.put("F", 0.0);
double points = 0.0;
for (int i = 0; i < gradesArray.length; i++) {
System.out.println("Enter grade (include + or -) ");
String grade = input.nextLine();
gradesArray[i] = grade;
//If the grades Map contains the inputted grade, use Map.get(grade) to obtain the value and add that to the points Double.
//Otherwise print invalid grade
if (grades.containsKey(grade)) {
points += grades.get(gradesArray[i]);
} else {
System.out.println("Invalid grade");
}
}
System.out.println("GPA: " + points / gradesArray.length);
Upvotes: 1
Reputation: 67
You could do something similar to this as well. You can use a switch block. points
needs to be brought outside the loop or it will just get reset to 0.0 every time a user enters a grade.
To make the user start over if it is an invalid grade you could use a recursive call. This means after the the "Invalid grade" message you would call the method that starts the process over again. This would require a little more refactoring and separating some of this code into more methods.
Scanner input = new Scanner(System.in);
double points = 0.0;
System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();
String[] gradesArray = new String[length];
for(int i = 0; i < gradesArray.length; i++){
System.out.println("Enter grade (include + or -) ");
gradesArray[i] = input.nextLine();
String grade = gradesArray[i].toUpperCase();
switch (gradesArray[i]) {
case "A+":
points += 4;
break;
case "A":
points += 4;
break;
case "A-":
points += 3.7;
break;
case "B+":
points += 3.3;
break;
default:
System.out.println("Invalid grade");
break;
}
}
System.out.println("GPA: " + points / gradesArray.length);
Upvotes: 1
Reputation:
All you need is declare points as a global variable and print out the GPA outside the for loop
public class Grade {
public static void main(String[] args){
double points=0.0;
Scanner input = new Scanner(System.in);
System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();
String[] gradesArray = new String[length];
for(int i = 0; i < gradesArray.length; i++)
{
System.out.println("Enter grade (include + or -) ");
gradesArray[i] = input.nextLine();
if(gradesArray[i].equalsIgnoreCase("A+") || gradesArray[i].equalsIgnoreCase("A"))
{
points += 4;
}
else if(gradesArray[i].equalsIgnoreCase("A-"))
{
points+= 3.7;
}
else if(gradesArray[i].equalsIgnoreCase("B+"))
{
points += 3.3;
}
else if(gradesArray[i].equalsIgnoreCase("B"))
{
points += 3.0;
}
else if(gradesArray[i].equalsIgnoreCase("B-"))
{
points += 2.7;
}
else if(gradesArray[i].equalsIgnoreCase("C+"))
{
points += 2.3;
}
else if(gradesArray[i].equalsIgnoreCase("C"))
{
points += 2.0;
}
else if(gradesArray[i].equalsIgnoreCase("D"))
{
points += 1.0;
}
else if(gradesArray[i].equalsIgnoreCase("F"))
{
points += 0.0;
}
else
{
System.out.println("Invalid grade");
}
}
System.out.println("GPA: " + points / gradesArray.length);
}
Upvotes: 0
Reputation: 8338
You're very close. You need to add another bracket before your println
to close out your forloop
. You only want to calculate GPA once all of the grades are entered. So like this:
And, as Jason mentioned in the comments, you need to make sure to create points
outside of the loop. Otherwise, you won't be able to access it afterwards to get calculate the full GPA.
double points = 0.0; //this has to go out here to be able to access it later
for(int i = 0; i < gradesArray.length; i++) {
System.out.println("Enter grade (include + or -) ");
gradesArray[i] = input.nextLine();
...
else if(gradesArray[i].equalsIgnoreCase("F")) {
points += 0.0;
} else {
System.out.println("Invalid grade");
}
}
System.out.println("GPA: " + points / gradesArray.length);
As for resetting input, this is easy as well. You can just use a while
loop - so do something like this.
boolean inputNeeded = true;
while(inputNeeded) {
System.out.println("Please enter grades:);
String grade = scan.nextLine();
if(grade_is_valid_check_here) {
inputNeeded = false;
} else {
System.out.println("Input is invalid - please try again");
}
}
Upvotes: 2