1

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.

Julian Lachniet
  • 128
  • 1
  • 18
  • Verify the data before its put into the list. – Carcigenicate May 04 '19 at 18:33
  • Although, this problem may have to do with you using `==` to compare Strings. You haven't included enough background to know if that's the case though. Strings need to be compared using `equals`. Make that change and see what happens. – Carcigenicate May 04 '19 at 18:34
  • @Carcigenicate I changed it, but it didn't make a difference. I've been testing using the first option, then the sixth. Neither of those sections had == to compare strings, so I don't see why it would make a difference. – Julian Lachniet May 04 '19 at 18:37
  • did you test `in.nextLine()` works in choice 1? – matanper May 04 '19 at 18:44
  • You need to read this https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo and this https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class to fix your issue. Because both choice 1 & choice 2 has bugs similar to above links. So try to resolve yourself by reading above two links. – Abhishek May 04 '19 at 19:05

2 Answers2

1

You didn't describe your input, but it looks like the problems is in choice 1. you have two calls to scanner, in.next() which returns the first word from the input (until space) and in.readLine() which returns a full line from the input.
To fix just remove the in.next()

For more info you can look at What's the difference between next() and nextLine() methods from Scanner class?

matanper
  • 801
  • 7
  • 19
1

It's not showing anything because of the way you are receiving your input. Let me try to explain. When you write in.next(), the scanner tries to read the word on your input terminal before a space. So lets say you entered Peter as the name of the student, in.next() will read Peter but since you didn't assign it to any variable, it won't be used. Then you did String name = in.nextLine(), this will try to read the input in the nextlline of your terminal, which will be an empty string because you didn't give it any input.

For your code to work. Write

  String name = in.next();

Remove

 String name = in.nextLine();

It should work now

Note, the size of the array increases because empty strings are being added to it.

Lundii
  • 99
  • 4