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

  • Context: Comp Sci 
  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Java List Program
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to user input handling in a program designed to manage a list of chores. Participants explore the behavior of input methods and their impact on program flow.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant describes the problem of the menu displaying again before adding or removing items, seeking assistance.
  • Another participant suggests that the issue arises from the interaction between keyboard.nextInt() and keyboard.nextLine(), explaining that nextInt() skips whitespace and leaves a newline character in the input buffer.
  • The proposed solution involves adding a keyboard.nextLine() call immediately after keyboard.nextInt() to consume the newline character, allowing the subsequent nextLine() call to capture the intended user input.
  • A later reply confirms that the suggested fix resolved the original issue.

Areas of Agreement / Disagreement

Participants generally agree on the cause of the issue and the proposed solution, with one participant confirming the effectiveness of the fix.

Contextual Notes

The discussion does not delve into potential edge cases or other input handling methods that may affect program behavior.

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 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K