-1

So I'm making a school database where you can enter a Student's information (Name, ID, Grade) through a class, but I just ran into a problem, how would I make a loop for someone to enter a student's id, name and grade? this is how the code looks if I were to write it before runtime (I will be using util.Scanner)

Student john = new Student(1, "John", 4)

It will then be added to an ArrayList

List<Student> studentsList = new ArrayList<>();
studentsList.add(john);

My problem is how will I name the variable be so it can be added to the list since there is no dynamic variables in java? This is a rough code of what I'm trying to do

for(int x = 0; x <= 10; x++){
   int id, grade;
   String name;
   System.out.println("Name: ");
   name = input.nextLine();
   System.out.println("ID: ");
   id = input.nextInt();
   System.out.println("Grade: ");
   grade = input.nextInt();

   Student ??? = new Student(id, name, grade);
   studentsList.add(???);
}
Mirax
  • 1
  • 1
  • 2
    It doesn't matter *what* you name it, you can name it `fred` or `george` or `foo`, and the result will be the same. You are worrying about the wrong thing as a local variable's name is not important and doesn't exist outside of the loop. Even better, *don't* give the local variable a name: `studentsList.add(new Student(id, name, grade));`, and *again* the result will be the same – Hovercraft Full Of Eels May 15 '22 at 22:14
  • 2
    You should read [this question](https://stackoverflow.com/q/13102045) - because you're about to get bitten by the issue that it describes. – Dawood ibn Kareem May 15 '22 at 22:46
  • @DawoodibnKareem Yeah I already know about that one, didn't want to make the question lengthy that's why I used nextLine before nextInt haha ;) – Mirax May 15 '22 at 22:51
  • @HovercraftFullOfEels problem is, there are some methods that I would like to use like getName and such, so I need to know the name.. I might just have to scratch this whole project ahaha – Mirax May 15 '22 at 22:53
  • 1
    As @Hovercraft pointed out, you can give it whatever name you like. The name disappears after the next `}` character anyway. So please, write `Student hovercraft = new Student(id, name, grade); studentsList.add(hovercraft);`. It'll be fine. And the other problem that I mentioned _is_ going to bite you, on the second iteration of the loop. – Dawood ibn Kareem May 15 '22 at 23:06
  • You seem greatly confused. The variable name has no meaning, doesn't even exist, after the for loop has ended. You may be giving much greater importance to this than it really deserves. – Hovercraft Full Of Eels May 15 '22 at 23:27
  • Perhaps thos perspective will help. You have a List of Student named `studentsList`, for example: `List studentsList = new ArrayList<>();`. Each element you **add** to this list represents an instance of Student, in other words, think of the List element as being your instance variable name for the Student that was added. So, to add a new Student to the List you would simply have: `studentsList.add(new Student(id, name, grade));` To view all your current Student instances in the List you can do: `for (Student student : studentsList) { System.out.println(student.getName()); }`. – DevilsHnd May 16 '22 at 00:55

0 Answers0