I'm working on a homework problem. Right now, I have a List of names. I have a function that should add the name to the List, and another one that should get it. However, it just gets an empty string
I tried debugging by getting the size() of the array, which increases when I add to it, but I can't get the contents of the item I added (if it's even there)
import java.util.List;
import java.util.Scanner;
public class main
{
public static void main(String args[]) {
List<String> studentNames = new ArrayList<>();
List<List<Integer>> studentScores = new ArrayList<>();
List<String> assignmentNames = new ArrayList<>();
List<Integer> assignmentMaxPoints = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("");
System.out.println("----------------");
System.out.println("1) New Student");
System.out.println("2) Edit Student Name");
System.out.println("3) Delete Student");
System.out.println("4) New Assignment");
System.out.println("5) View Student");
System.out.println("6) View Averages");
System.out.println("----------------");
if (0 != studentNames.size()) {
System.out.println(studentNames);
}
int choice = in.nextInt();
if (choice == 1) {
System.out.println("");
System.out.println("----------------");
System.out.println("Enter the student name:");
System.out.println("----------------");
in.next();
String name = in.nextLine();
studentNames.add(name);
}
if (choice == 2) {
System.out.println("");
System.out.println("----------------");
System.out.println("Enter the old student name:");
System.out.println("----------------");
in.next();
String oldName = in.nextLine();
System.out.println("");
System.out.println("----------------");
System.out.println("Enter the new student name:");
System.out.println("----------------");
in.next();
String newName = in.nextLine();
for (int nameIndex = 0; nameIndex < studentNames.size(); nameIndex++) {
if (studentNames.get(nameIndex).equals(oldName)) {
studentNames.set(nameIndex, newName);
}
}
}
if (choice == 3) {
System.out.println("");
System.out.println("----------------");
System.out.println("Enter the student name:");
System.out.println("----------------");
in.next();
String name = in.nextLine();
for (int nameIndex = 0; nameIndex < studentNames.size(); nameIndex++) {
if (studentNames.get(nameIndex).equals(name)) {
studentNames.remove(nameIndex);
}
}
}
if (choice == 6) {
System.out.println("");
for (int studentIndex = 0; studentIndex < studentNames.size(); studentIndex++) {
System.out.println(studentNames.get(studentIndex));
}
}
}
}
}
I expected the code for the sixth choice to print all the students in studentNames, but it only prints blank lines.