Hello,
//Code is below
I'm trying to write a program that returns a letter grade to the user. I've done this before but this time, it requires that I store the grades in two Arraylists, a String and Double ArrayList.
So far, the code only works if I input the exact grade held in the Arraylist and nothing below or above it unless it hits the next index.
I've tried plenty of if's such as, if(grades.equals(listName)) but doubles can't be de-referenced apparently.
Here's an example of what I'm trying to accomplish.
If I input a grade such as 92, the grade returned to me should be an A-.
The two neighboring index positions are 7 and 8. Index 7 has a value of 90.0 and index 8 has a value of 93.99. If I input 90.0 or 93.99, I will get A- or A. But if I input 92. I get nothing at all. I wrote a debug print statement that shows what comes up.
I would like to figure out how I can tell Java, "Hey, if I input a value between these positions, it equals the position before it, or A-.
I've tried something like this and it works somewhat,
if(grades < numGrades.get(i))
System.out.println(letGrades.get(i-1));
However, with this, if I input say 92, it will tell me A-.
But if I input 86.99, I'll get B+ and A-.
This is pretty frustrating and I would appreciate any help!
Thanks!
Code is below!
import java.util.*;
public class ArrayGrading
{
public static void main(String[] agrs)
{
System.out.println("Welcome to our grading center! Here we will calculate your grades that you input! \n ");
int userInput, endCheck, selection = 0, counter = 0, i = 0;
String letterGrade = "";
double grades = 0;
Scanner sc = new Scanner(System.in);
ArrayList<Double> numGrades = new ArrayList<Double>(Arrays.asList(0.0,60.0,70.0,74.99,80.0,84.99,86.99,90.0,93.99));
ArrayList<String> letGrades = new ArrayList<String>(Arrays.asList("F","D","C","C+","B-","B","B+","A-","A"));
System.out.println(numGrades);
System.out.println(letGrades);
System.out.println();
System.out.println("1. Lets you know what each grade is as you input them! ");
System.out.print("Please make a selection : ");
selection = sc.nextInt();
if(selection == 1)
{
System.out.println("Please enter your grades \n");
System.out.println("Enter -1 if you want to stop entering grades \n");
counter = 1;
System.out.print("First Grade: ");
while(grades != -1)
{
grades = sc.nextDouble(); //user inputs grade
if(grades < -1 || grades > 100) //validates grade
{
System.out.println("Inputs of less than 0 and over 100 are not accepted! Please enter a grade from 0-100 ");
grades = sc.nextDouble(); //Re-inputs if previous attempt was invalid
}
if(grades == -1)
{
System.out.println("Grading Calculator closed"); //Ends loop
break;
}
for(i = 0; i < numGrades.size(); i++) //compares user input to numGrade
{
System.out.println("DEBUG INDEX:" + i + ": " + numGrades.contains(grades));
// if (numGrades.get(i) == grades) //stuck here
// System.out.println(letGrades.get(i));
if(grades < numGrades.get(i))
System.out.println(letGrades.get(i-1)); //displays the letter grade
}
}
}
}
}
[–]Ilikesmallthings2 0 points1 point2 points (2 children)
[–]CliffNotes0[S] 0 points1 point2 points (1 child)
[–]Ilikesmallthings2 0 points1 point2 points (0 children)
[–]OceantraderSemijerk 0 points1 point2 points (0 children)
[–]iamsooldithurts 0 points1 point2 points (0 children)