1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java - list program

  1. Mar 10, 2012 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant 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 cant figure out why it is doing this. Thank you in advance.

    3. The attempt at a solution

    Code (Text):
    /*
     * 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;  
                }
            }
        }
    }
     
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Mar 10, 2012 #2
    bump, anyone have an idea?
     
  4. Mar 11, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  5. Mar 11, 2012 #4
    Thank you that solved my problem. :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - list program
  1. Java programming (Replies: 4)

  2. Java program (Replies: 3)

Loading...