Solved Java List Program Homework

In summary, the conversation was about a programming assignment to create a bag of strings for a list of chores. The user was having trouble with the menu repeating before being able to add or remove items from the list. The issue was caused by the way the keyboard functions worked and was solved by adding an extra call to keyboard.nextLine().
  • #1
schapman22
74
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
  • #2
bump, anyone have an idea?
 
  • #3
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.
 
  • #4
Thank you that solved my problem. :smile:
 
  • #5


I would like to commend you for taking the initiative to solve this Java list program homework. It shows your dedication to learning and improving your programming skills.

Regarding the issue you are facing, it seems like the program is displaying the menu again before allowing you to add or remove an item. This could be due to a few reasons. One possibility is that there is an extra line break or whitespace being read in by the scanner, causing it to skip over the user input for the chore. I would recommend checking your code for any potential whitespace or line break issues.

Another possibility is that you are using the nextInt() method to read user input, which only reads the integer value and not the entire line. This could cause issues when you try to read in the chore name using the nextLine() method. I would suggest using the nextLine() method consistently to ensure that the entire line is read in.

I hope these suggestions help you in resolving the issue and completing your homework successfully. Keep up the good work!
 

1. How do I create a Java list program for my homework?

To create a Java list program for your homework, you will need to first define the structure of your list using either an array or an ArrayList. Then, use loops and conditional statements to manipulate and access the elements in your list. Finally, make sure to test your program thoroughly to ensure it is functioning correctly.

2. What is the difference between an array and an ArrayList in Java?

An array is a fixed-size data structure that stores a collection of elements of the same data type, while an ArrayList is a dynamic data structure that can grow or shrink in size and can store elements of any data type. Arrays are more efficient in terms of memory and performance, but ArrayLists offer more flexibility in terms of adding and removing elements.

3. How do I add elements to a Java list program?

To add elements to a Java list program, you can use the add() method for ArrayLists or use a loop to assign values to each index in an array. For ArrayLists, you can also use the add(index, element) method to insert elements at a specific position.

4. How do I access elements in a Java list program?

To access elements in a Java list program, you can use the index notation ([]) to specify the position of the element you want to access. For example, to access the first element in an ArrayList, you would use list.get(0) or list[0]. For arrays, you can directly access an element using its index, such as array[2] to access the third element.

5. How do I remove elements from a Java list program?

To remove elements from a Java list program, you can use the remove() method for ArrayLists or use a loop to set the value of a specific index to null for arrays. For ArrayLists, you can also use the remove(index) method to remove a specific element at a given position.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top