0

This is my current code, fairly basic, I'm adding names to an array and deleting said names using scanners. Like I said basic stuff. I'm using a switch since it makes my life 100000x easier than just using a bunch of if's and it's a nicer layout regardless.

The following is my code:

import java.util.Scanner;
import java.util.ArrayList;

public class Account
{

public static void main (String [] args)
{
    Scanner sc = new Scanner (System.in);
    ArrayList list = new ArrayList();


    boolean quit = false;

    do
    {

        System.out.println ("MENU\n 0: Enter name to list\n 1: Delete name from list\n 2: Print Current List\n 3: Quit");
        int input = sc.nextInt();
        switch(input)
        {
            case 0:
                System.out.println ("Enter name to add to list");
                list.add(sc.nextLine());
                System.out.println("Name has been added");
                System.out.println (list);
                break;
            case 1:
                System.out.println("Enter name to remove");
                list.remove(sc.next());
                System.out.println (list);
                break;
            case 2:
                System.out.println (list);
                break;
            case 3:
                quit = true;
                break;
            default:
                System.out.println ("Invalid choice");
        }


    }while(!quit);
    System.out.println ("Exiting menu");


 }
 }

My issue is that once I enter 0 to go to the first case, it seems as though it just skips the user entry and outputs an empty string. If I were to do that exact same procedure outside of my switches it seems to work, so it makes me think that something might be up with my scanner. Possibly has something to do with the int scan I made right before to get into the case? But I could have sworn I've done something like this dozens of times...

I should also mention that sc.next() works fine, its just sc.nextLine() that seems to be giving me stress.

Uri D. Charles
  • 57
  • 1
  • 1
  • 9
  • 1
    Typically, if you are using `nextInt` or any of the other method other than `nextLine`, there will be a new line character left in the buffer which will be picked up the next time you try and "grab" something from it, which can cause all sorts of weirdness. Best to call `nextLine` after using one of those methods – MadProgrammer Dec 11 '14 at 05:42

2 Answers2

0

When you use a Scanner, if you want to use nextline() after next(), nextInt(), or nextDouble(), you have to put a nextLine() alone before the line where you get input, in order to consume the newline.

Or, to avoid having to account for this problem, instead of using nextInt(), and nextDouble(), just do:

Integer.parseInt(nextLine())//equivalent of nextInt() except no funny errors
Double.parseDouble(nextLine())//equivalent of nextDouble() without funny stuff
kirbyquerby
  • 735
  • 5
  • 16
0

This is happening because the nextInt does not read the last newline character of the input, and that remaining newline is getting consumed in the next call to nextLine. The same behavior can be noticed if we use nextLine after any of the nextXXX except for the nextLine itself

In order to solve this problem you can:

Either use a blank nextLine call after nextInt to consume the newline

int input = sc.nextInt();
sc.nextLine();  // Use this to consume the newline character
String input1 = sc.nextLine();

or you can use the workaround as mentioned by @kirbyquerby

Abhishek
  • 878
  • 12
  • 28