Comp Sci Why Does My Java Program Display the Menu Before Adding or Removing Items?

  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Java List Program
AI Thread Summary
The Java program displays the menu again before adding or removing items due to the way the Scanner methods handle input. Specifically, after using keyboard.nextInt(), the subsequent call to keyboard.nextLine() captures the leftover newline character, resulting in an empty string being processed. To resolve this, it's recommended to add keyboard.nextLine() immediately after keyboard.nextInt() to clear the newline. This adjustment ensures that the program correctly prompts for user input without skipping lines. Implementing this fix resolves the issue effectively.
schapman22
Messages
74
Reaction score
0

Homework Statement



I am supposed to write a program that uses a bag of strings to keep track of a list of chores. The user can add, remove, view, etc. items on the list.

Homework Equations



When I run the program and i choose an option from the menu. It displays the menu again before you can add or remove the item you want. I can't figure out why it is doing this. Thank you in advance.

The Attempt at a Solution



Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.colorado.collections;
import java.util.Scanner;


/**
 *
 * @author Scott
 */
public class Main 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        ArrayBag<String> choreList = new ArrayBag<>();
        int entry;
        String chore;
        boolean endProgram=false;
        Scanner keyboard = new Scanner(System.in);
        
        while(!endProgram)
        {
            System.out.println("Choose an option from the following list.");
            System.out.println();
            System.out.println("Add item to list(press 1)");
            System.out.println("View number of items on list(press 2)");
            System.out.println("View a list of chores(press 3)");
            System.out.println("Remove an item from the list(press 4)");
            System.out.println("Exit program(press 5)");
            entry = keyboard.nextInt();

            switch(entry)
            {
                case 1: System.out.println();
                    System.out.println("What would you like to add?");
                    chore = keyboard.nextLine();
                    choreList.add(chore);
                    break;
                case 2: System.out.println();
                    System.out.println("There are " + choreList.size() +
                        " items on your list.");
                    break;
                case 3: System.out.println();
                choreList.printList();
                    break;
                case 4: System.out.println();
                    System.out.println("Which item would you like to remove?");
                    chore = keyboard.nextLine();
                    choreList.remove(chore);
                    break;
                case 5: endProgram=true;
                    break;   
            }
        }
    }
}
 
Physics news on Phys.org
bump, anyone have an idea?
 
I don't know Java, but took a quick look at keyboard.nextInt() and keyboard.nextLine(). The issue seems to be the way these functions work. keyboard.nextInt(), will skip any whitespace until it reaches the next string of digits and in this case it stops just before a new line character that the user already entered. Then when you call keyboard.nextLine(), it sees the newline character that was already entered and returns with a null string, causing the issue you're having.

The fix is to add keyboard.nextLine() immediately afterthe keyboard.nextInt(), in which case the second call to keyboard.nextLine() will get the data you want.

The reason this isn't a problem when using keyboard.nextInt() twice in a row is that nextInt() considerd newline to be the same as any whitespace character, and just skips past it to look for another string of digits, which will result in getting another line of input from the user.
 
Thank you that solved my problem. :smile:
 

Similar threads

Replies
2
Views
4K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
7
Views
3K
Replies
6
Views
3K
Replies
3
Views
8K
Replies
2
Views
3K
Replies
4
Views
3K
Back
Top