What am I doing wrong with this program? - java

I'm trying to write a code to convert a letter grade to a numerical value. I have this code but when I run it, I get an error saying java.lang.StringIndexOutOfBoundsException.
What am I doing wrong?
Here is the code:
import java.util.Scanner;
public class Lab6Smalls {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String grade;
String letter1;
String letter2;
double gpa = 0;
System.out.print("Enter a letter grade: ");
grade = scan.nextLine();
letter1 = grade.substring(0,1);
letter2 = grade.substring(1,2);
if (letter1.equals("A") && letter2.equals("+")){
gpa = 4.3;
} else if (letter1.equals("A")){
gpa = 4.0;
} else if (letter1.equals("B")){
gpa = 3.0;
} else if (letter1.equals("C")){
gpa = 2.0;
} else if (letter1.equals("D")){
gpa = 1.0;
} else if (letter1.equals("F")){
gpa = 0.0;
} else {
System.out.println("The value is invalid.");
}
if (!letter1.equals("F")){
if(letter2.equals("+") && !letter1.equals("A")) gpa += 0.3;
else if(letter2.equals("-")) gpa -= 0.3;
}
System.out.println("The numerical value is " + gpa + ".");
}
}

There is an error because alphabets other than A+ do not have a second value
A+ has two letters
B has one so when you try to find a second, it breaks
C has one so when you try to find a second, it breaks
D has one so when you try to find a second, it breaks
You want to check if the second value exists, before you retrieve it.
Try this
String letter2 = "";
grade = scan.nextLine();
letter1 = grade.substring(0,1);
// Check, if there is a second letter, get it.
if (grade.length() >= 2) {
letter2 = grade.substring(1,2);
}

Before you substring(1, 2) you should make sure the length > 1.
The following is what you want.
import java.util.Scanner;
public class Lab6Smalls {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String grade;
double gpa;
System.out.print("Enter a letter grade: ");
grade = scan.nextLine();
if (null == grade || "".equals(grade)) {
System.out.println("The value is invalid.");
return;
}
switch (grade.substring(0, 1)) {
case "A":
gpa = 4.0;
break;
case "B":
gpa = 3.0;
break;
case "C":
gpa = 2.0;
break;
case "D":
gpa = 1.0;
break;
case "F":
gpa = 0.0;
break;
default:
System.out.println("The value is invalid. : " + grade);
return;
}
if (grade.length() > 1) {
String letter2 = grade.substring(1, 2);
if ("+".equals(letter2)) {
gpa += 0.3;
} else if ("-".equals(letter2)) {
gpa -= 0.3;
}
}
System.out.println("The numerical value is " + gpa + ".");
}
}

Related

Enter is required twice in my GPA Calculator

I barely started coding a couple weeks ago and have a project for my high school computer science class that requires me to make a GPA calculator. It's not the best code right now or the best technique to calculate GPA but it's a start (if someone could help clean it up and simplify it I would greatly appreciate you).
There's a problem with the code though. Every time a user input is required from the console they have to press enter twice. I feel like the solution is very easy but I am stuck on why this is happening.
import java.util.Scanner;
public class gpaCalc {
public static double gpa;
public static String grade = "";
public static double gpaTotal = 0;
public static double points = 0;
public static double numberOfClasses;
public static double class1points;
public static double class2points;
public static double class3points;
public static double class4points;
public static double class5points;
public static double class6points;
public static double class7points;
public static double class8points;
static Scanner letterGrade = new Scanner(System.in);
static Scanner numClasses = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Adrian's GPA calculator.");
System.out.println("This program will calculate your weighted and unweighted GPA for this school year.");
classesNumber();
}
public static void classesNumber() {
System.out.println("Please enter the number of classes that you are enrolled in for the school year.");
numberOfClasses = numClasses.nextDouble(); //input is stored in variable numberOfClasses
if(numberOfClasses >= 9 || numberOfClasses < 2)
System.out.println("Are you sure you go to ECHS?");
//limit the number of classes from 2-8
if (numberOfClasses <= 8 && numberOfClasses >= 2)
System.out.println("Please enter the letter grade you have recieved in class 1 (Uppercase letters i.e. A,B,C,D,F).");
else
classesNumber(); //error occurs and doesn't reset when a letter is put and not a number
class1();
}
public static void class1() {
grade = letterGrade.next(); //unweighted GPA finder
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class1 ();
}
class1points = points;
class2();
}
public static void class2() {
System.out.println("Please enter the letter grade you have recieved in class 2 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class2();
}
class2points = points;
class3();
}
public static void class3() {
System.out.println("Please enter the letter grade you have recieved in class 3 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class3();
}
class3points = points;
class4();
}
public static void class4() {
System.out.println("Please enter the letter grade you have recieved in class 4 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class4();
}
class4points = points;
class5();
}
public static void class5() {
System.out.println("Please enter the letter grade you have recieved in class 5 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class5();
}
class5points = points;
class6();
}
public static void class6() {
System.out.println("Please enter the letter grade you have recieved in class 6 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class6();
}
class6points = points;
class7();
}
public static void class7() {
System.out.println("Please enter the letter grade you have recieved in class 7 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class7();
}
class7points = points;
class8();
}
public static void class8() {
System.out.println("Please enter the letter grade you have recieved in class 8 (Uppercase letter i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class8();
}
class8points = points;
calculator();
}
public static void calculator() {
gpaTotal = (class1points + class2points + class3points + class4points + class5points + class6points + class7points + class8points ) / numberOfClasses;
System.out.println("Your unwweighted GPA is: " + gpaTotal);
}
}
Your problem should be solved by removing 1 scanner, and suggestions to your codes :
Use array to store subjects
String[] mySubjects = {"sub_1", "sub_2", "sub_3", "sub_4", ..};
Use loop to reduce repetitive
for (int i = 0; i < mySubject.length; i++) {
your_code_here;
}
Created a simplified version of your code, without need for an array.
Maybe not the best, but if you just started out, it can guide you somewhere about avoiding code repetition.
I tried to organize everything the way you did, just to help you see the code more clearly.
Maybe you can add a plus check to this code, in the classNumber() method, to avoid entering non integer values. + Split the getClassPoints() method into some parts to avoid side effects..
public static char grade;
public static double gpaTotal = 0;
public static int numberOfClasses;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Adrian's GPA calculator.");
System.out.println("This program will calculate your weighted and unweighted GPA for this school year.");
classesNumber();
}
public static void classesNumber() {
System.out.println("Please enter the number of classes that you are enrolled in for the school year.");
numberOfClasses = scan.nextInt(); //input is stored in variable numberOfClasses
if(numberOfClasses >= 9 || numberOfClasses < 2)
System.out.println("Are you sure you go to ECHS?");
getClassPoints();
}
public static void getClassPoints () {
//You dont need the numberOfClasses as parameter because its static.
for (int i = 0; i < numberOfClasses; i++){
while (true){
System.out.println("Please enter the letter grade you have recieved in class " + (i + 1) +"(Uppercase letter i.e. A,B,C,D,F).");
grade = scan.next().charAt(0); //unweighted GPA finder
grade = Character.toLowerCase(grade);
if (grade == 'a'){
gpaTotal += 4;
break;
}
else if (grade == 'b'){
gpaTotal += 3;
break;
}
else if (grade == 'c'){
gpaTotal += 2;
break;
}
else if (grade == 'd'){
gpaTotal += 1;
break;
}
else if (grade == 'f'){
break;
}
System.out.println("Invalid entry. Please enter a valid entry.");;
}
}
if (gpaTotal > 0){
System.out.println("Your unwweighted GPA is: " + (gpaTotal / numberOfClasses));
}
else {
System.out.println("Too many F grade!");
}
}
}

How to call same method multiple times to add something

So im creating a gpa calculator, it takes the 4 classes (or more if i want) and calculates the letter grade and gpa weight.
How would i use the same method 4 times and each time have it add the different inputted variables.
For example, the first input of the first calling (for the grade) is an 80.
if i call it again and input a 90, I want it to store the inital 80 and add the 90 and so on so i can calculate the gpa.
Here is my code
import java.util.*;
public class GpaCalculator{
public static void main(String[] args) {
{
System.out.println("Please enter your name>>>");
Scanner stringinputs = new Scanner(System.in);
String name = stringinputs.nextLine();
GreatGrader();
System.out.println();
System.out.println(name);
}
}
public static void GreatGrader(){
Scanner stringinput = new Scanner(System.in);
System.out.println("Please enter your course>>>");
String Course1 = stringinput.nextLine();
System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>");
String weight = stringinput.nextLine();
System.out.println("What is your grade in the class?>>>");
String grade = stringinput.nextLine();
double gpa = 0;
if (weight.equalsIgnoreCase("honors"))
gpa = gpa + 1;
else if (weight.equalsIgnoreCase("Ap"))
gpa = gpa + 1.5;
else if (weight.equalsIgnoreCase("normal"));
String letter;
Scanner in = new Scanner(System.in);
int grades = in.nextInt();
if (grades >= 96)
{ gpa = 4.5;
letter = "A";}
else if (grades >= 92)
{ gpa = 4.0;
letter = "A";}
else if (grades >=88 )
{ gpa = 3.5;
letter = "B"; }
else if (grades >= 84 )
{ gpa = 3.0;
letter = "B";}
else if (grades >=76 )
{ gpa = 2.5;
letter = "C";}
else if (grades >= 72)
{gpa = 1.5;
letter = "C";}
else if (grades>=68)
{ gpa = 1.0;
letter = "D";}
else if (grades <=67)
{ gpa = 0;
letter = "F";
}
}
}

GPA array calculation error

I am having issues with my gpa program where I have to use two arrays to store a grade and it's credits and calculate the gpa. So far, everything else seems to be working except the gpa wont calculate correctly and I am not sure what I am missing (probably simple).
My code thus far is:
Gpa class:
import java.util.*;
public class Gpa{
int[] credits = new int[4];
String[] grades = new String[4];
private int numCourses;
private int maxCourses;
private int sumOfCourses;
private int sumCredits;
private int sumPoints;
int newCredits;
int totalSum = 0;
int total = 0;
public Gpa(int noCourses){
maxCourses = noCourses;
numCourses = 0;
}
public void addCourse(int _newCredits, String newGrade){
for (int i=0; i<maxCourses; i++){
newCredits = _newCredits;
credits[i] = newCredits;
}
for (int i=0; i<maxCourses; i++){
grades[i] = newGrade;
}
switch (newGrade) {
case "A":
case "a":
newGrade = "4";
break;
case "B":
case "b":
newGrade = "3";
break;
case "C":
case "c":
newGrade = "2";
break;
case "D":
case "d":
newGrade = "1";
break;
case "F":
case "f":
newGrade = "0";
break;
}
sumPoints = sumPoints + (newCredits * Integer.parseInt(newGrade));
numCourses++;
}
public double calcGPA(){
for (int i=0; i<maxCourses; i++){
sumCredits = sumCredits + newCredits;
}
double gpa = (double)sumPoints/sumCredits;
return gpa;
}
} // end class
The tester class:
import java.util.Scanner;
public class GpaTestEx2
{
public static void main (String[] args)
{
//declarations
Scanner in = new Scanner(System.in); //input object
int numCourses; //number of courses - can be changed
int credits; //number of credits for a course
String grade; //grade for course
//read in number of courses
System.out.print("Enter number of courses: ");
numCourses = in.nextInt();
//create Gpa object to hold specified number of courses
Gpa myGPA = new Gpa(numCourses);
//read in all courses and add course information to Gpa object
for (int k=0; k<numCourses; k++)
{
System.out.print("Enter credits for course " + (k+1) + ": ");
credits = in.nextInt();
System.out.print("Enter grade for course " + (k+1) + ": ");
grade = in.next();
myGPA.addCourse(credits, grade);
}
//print results
System.out.println();
System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( ));
} //end main
}
When I enter the course credits and grade it does not calculate the gpa correctly. For example, if the user input says there are 2 courses with one class having 4 credits with a grade of A and the other having 3 credits with a grade of B. I get a gpa of around 4.17 when it should be 3.57.
Any help would be great, I may or may not be missing something simple.
It seems like you are indexing into every element in your array every time someone inserts a value. We only need to change one element when a new grade is added. GPA class:
public class Gpa {
private int[] credits;
private String[] grades;
private int currentGrade;
public Gpa(int numGrades) {
credits = new int[numGrades];
grades = new String[numGrades];
currentGrade = 0;
}
public void addGrade(String letterGrade, int credit) {
grades[currentGrade] = letterGrade;
credits[currentGrade] = credit;
currentGrade = currentGrade + 1;
}
public double getGpa() {
double totalPoints = 0;
double totalWeight = 0;
for (int i = 0; i < currentGrade; i++) {
totalPoints = totalPoints + (letterToGpa(grades[i]) * credits[i]);
totalWeight = totalWeight + credits[i];
}
return totalPoints / totalWeight;
}
private double letterToGpa(String letter) {
char first = letter.toUpperCase().charAt(0);
switch (first) {
case 'A':
return 4.0;
case 'B':
return 3.0;
case 'C':
return 2.0;
case 'D':
return 1.0;
}
return 0.0;
}
}
The test class should work fine now:
public static void main(String[] args) {
Scanner in = new Scanner(System.in); //input object
int numCourses; //number of courses - can be changed
int credits; //number of credits for a course
String grade; //grade for course
//read in number of courses
System.out.print("Enter number of courses: ");
numCourses = in.nextInt();
//create Gpa object to hold specified number of courses
Gpa t = new Gpa(numCourses);
//read in all courses and add course information to Gpa object
for (int k=0; k<numCourses; k++)
{
System.out.print("Enter credits for course " + (k+1) + ": ");
credits = in.nextInt();
System.out.print("Enter grade for course " + (k+1) + ": ");
grade = in.next();
t.addGrade(grade, credits);
}
//print results
System.out.println();
System.out.printf("GPA is %4.2f%n", t.getGpa());
}
Just want to note that this clearly isn't the best way of doing this, nor does it follow Object Oriented patterns very well, but the OP's assignment requires we only use one class, etc.

Not understanding how to make my for loop and if statement work

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.
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");
}
}
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);
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);
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);
}

Scanner only reads for loop once? - Java

I'm trying to make a class where you input any number of grades (A-F) and calculates the GPA and returns the GPA and eligibility to extracurricular activities. It seems like the scanner only allows one input, then prints the GPA and eligibility.
So far this is what I have:
import java.util.Scanner;
public class Grades
{
public static void main(String[] args)
{
double myGPA;
int myNumClasses;
double myValue;
Scanner sc = new Scanner(System.in);
System.out.println("Press any other lettter to calculate.");
System.out.print("Enter grades: ");
String input = sc.nextLine();
myValue = 0;
myNumClasses = 0;
myGPA = 0;
for (String next = sc.next(); input.equalsIgnoreCase("a") || input.equalsIgnoreCase("b") ||
input.equalsIgnoreCase("c")|| input.equalsIgnoreCase("d") || input.equalsIgnoreCase("f"); next = sc.next())
{
if (input.equalsIgnoreCase("a"))
{
myValue += 4.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("b"))
{
myValue += 3.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("c"))
{
myValue += 2.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("d"))
{
myValue += 1.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("f"))
{
myNumClasses += 1;
}
myGPA = myValue / myNumClasses;
if (myGPA >= 2.0 && myNumClasses >= 4)
{
System.out.println("Eligible");
}
else if (myNumClasses < 4)
{
System.out.println("Ineligible, taking less than 4 classes");
}
else if (myGPA >= 2.0 && input.equalsIgnoreCase("f"))
{
System.out.println("Ineligible, gpa above 2.0 but has F grade");
}
else if (myGPA <= 2.0 && input.equalsIgnoreCase("f"))
{
System.out.println("Ineligible, gpa below 2.0 and has F grade");
}
else if (myGPA < 2.0)
{
System.out.println("Inelligible, gpa below 2.0");
}
System.out.println("Your GPA = " + myGPA);
}
}
}
// It looks like your missing a for loop. I just copied some of your
//code and ran it through a for loop. The rest of the code is kind of unclear.
System.out.println("Enter the number of grades you will enter: ");
int userAns = sc.nextInt();
for (int index = 0; index <= userAns; index++)
{
System.out.println("Press any other letter to calculate.");
System.out.print("Enter grades: ");
String input = sc.nextLine();
}

Categories

Resources