So I'm trying to create a program that takes user input for a student's name and GPA and inputs the information into a single arraylist. Pretty much, I'm trying to create an ArrayList that stores both a float and a string variable and is storing user input. I'm trying to use a constructor to accomplish this task, but I'm having trouble getting my code to work. Here's what I have so far:
public class BestStudent {
static Scanner scanner = new Scanner(System.in);
static String name;
static float gpa;
private class Student {
public Student (String n, float g){
name = n;
gpa = g;
}
}
public static void findValidictorian(){
ArrayList<Student> validictorian = new ArrayList<Student>();
while (true){
System.out.println("Please enter student name: ");
name.add(scanner.next());
System.out.println("Please enter your student's cumulative GPA: ");
gpa.add(scanner.nextFloat());
System.out.println("do you want to add another student yes/no?");
String answer = scanner.next();
if (answer.toLowerCase().equals("no")){
break;
}
}
}
public static void main(String[] args) {
findValidictorian();
}
}
I am getting an error on both of my add methods for my ArrayList, and I can't for the life of my figure out why.
nameandgpafields within the inner class itself, not in the outer class.