-1

I'm trying to build a text based to do list in Java but I'm having some difficulties when it comes to displaying the items.

When I run the code and enter "1" the contents of the to do list are displayed back to me, but they keep looping and they never stop. I'm assuming this has something to do with the while loop that checks the userChoice variable but my question is why does the list keep reiterating even after the break statement? What I'd like to have happen is to enter a number, have the action performed, and then have the instruction prompt displayed again.

java code:

package com.company;

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

public class Main {

// create an arraylist to store users items
static ArrayList<String> toDoList = new ArrayList<String>(3);


public static void main(String[] args) {

    // greet the user
    System.out.println("**Your To-Do list** \n");

    // add default items to list
    toDoList.add("Buy Groceries");
    toDoList.add("Work Out");
    toDoList.add("Play CS");

    // user menu/instruction
    System.out.println("Please select from one of the following options: \n 1. Show to-do list \n 2. Add item " +
            "\n 3. Remove item \n 4. Exit program \n");

    // prompt user for their choice
    System.out.print("Enter your choice: ");

    // get user choice
    Scanner input = new Scanner(System.in);
    int userChoice = input.nextInt();

    while (userChoice != 4) {
        switch (userChoice) {
            case 1:
                getToDoList();
                break;

            case 2:
                // create method that allows you to add item to the toDolist
                break;

            case 3:
                // create method that allows you to remove item from the toDolist
                break;

            case 4:
                // create method that terminates application
                break;
        }
    }

}

// method that returns contents of the list
public static void getToDoList(){

    for (int i = 0; i < toDoList.size(); i++) {
        System.out.println(toDoList.get(i));
    }
}

}
ClarkTheCoder
  • 179
  • 4
  • 17
  • Possible duplicate of [Java How can I break a while loop under a switch statement?](https://stackoverflow.com/questions/22823395/java-how-can-i-break-a-while-loop-under-a-switch-statement) – Joe C Jan 11 '18 at 07:14

3 Answers3

0

It is because you did not request to read from user again.

case 1:
    getToDoList();
    input.nextInt();
    break;

Or move the input.nextInt(); outside of switch block (below it).

Jai
  • 8,165
  • 2
  • 21
  • 52
0

Because userChoice is always 1, so it always loop.

-1
import java.util.ArrayList;
import java.util.Scanner;

public class Groceries {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList<String> a=new ArrayList<String>();
        a.add("Food");
        a.add("Furniture");
        a.add("Plywood");



        while(true)
        {

            System.out.println("Enter your choice");
            System.out.println("Your choice List\n 1:getList 2.addinthelist 3.removefromlist 4.exit");

            Scanner sc=new Scanner(System.in);
            int a1=sc.nextInt();


            switch(a1)
            {
            case 1:
                System.out.println(a);
                break;

            case 2:
                System.out.println("List before addition of elemnt is :"+a);
                System.out.println("Enter element to be added into the string");
                String sss=sc.next();

                a.add(sss);
                System.out.println("List after addition of element is :"+a);

                    break;

            case 3:
                System.out.println("List before deletion of elemnt is "+a);
                System.out.println("Enter an index of an element to be removed");
                int abc=sc.nextInt();
                a.remove(abc);
                System.out.println("List after Deletion of an element is "+a);
                break;

            case 4:
                System.exit(0);


            default:
                System.out.println("You entered wrong number !! Please enter 4 to exit");

            }
        }



    }



}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Yogesh
  • 155
  • 1
  • 9