0
`System.out.println();
        ArrayList<String> runescape = new ArrayList<String>();

        while (true) {
            display();

            int optionNum = input.nextInt();
            
            if (optionNum == 1) {
                System.out.print("Enter a String : ");
                runescape.add(input.next());
                System.out.println("Element Added");
            } 
            else if (optionNum == 2) {
                
                System.out.print("Enter String to remove :");
                String remov = input.next();
                if (runescape.contains(remov)) {
                    runescape.remove(new String(remov));
                    System.out.print("The String been removed.");
                }else
                    System.out.println("That String does not exist");
            } else if (optionNum == 3) {
                System.out.print("Your list: " + runescape);
            } else if (optionNum == 4) {
                System.out.print("You have exited the program.");
                break;
            }

        }`

I am wondering why I get this error, if I try entering a String made up of multiple words. For example if I enter "Hello Peter".

Thank you for the help in advance.

JOHN
  • 1
  • 1
    `Scanner#next()` will only read the _next token, up to the specified delimiter_. The default delimiter is a whitespace. So if you enter "Hello Peter", `next()` will only read "Hello". "Peter" is then still unread in the scanners buffer. Use `nextLine()` to read the whole line. However, if you use `nextLine()`, be careful if you mix `next()` and `nextLine()`, see [here](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). – maloomeister Jan 10 '23 at 09:34

1 Answers1

0

As stated by maloomeister, Scanner.next() will only read a single word (unless you have a different delimiter). To read a full line (all words), use Scanner.nextLine():

runescape.add(input.nextLine());
Fastnlight
  • 922
  • 2
  • 5
  • 16