1

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.

2
  • Did you perhaps intend to create a new BestStudent object, set its fields with what you read from the scanner, then add it to the list? Commented Apr 9, 2013 at 21:29
  • You should store the name and gpa fields within the inner class itself, not in the outer class. Commented Apr 9, 2013 at 21:30

2 Answers 2

3

validictorian is an ArrayList, but you're trying to add to gpa and name, which are not ArrayLists, and you never add to validictorian. I think what you want is more like

name = scanner.next();
...
gpa = scanner.nextFloat();
validictorian.add(new Student(name, gpa));
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what was wrong. I just learned Arraylists today, and boy do I feel stupid. Thanks!
0

name and gpa are not array lists, you can't add elements to them. The only ArrayList in sight is called validictorian (sic.) and anyway, you can't add elements of different types to an ArrayList (well, you could add them if the ArrayList is parameterized to use type Object, but that's a bad practice.)

Notice that validictorian is supposed to contain objects of type Student. What you meant to do all along was this:

System.out.println("Please enter student name: ");
String aName = scanner.next();
System.out.println("Please enter your student's cumulative GPA: ");
float aGpa = scanner.nextFloat();
validictorian.add(new Student(aName, aGpa));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.