My assignment is to create a program that asks the user what they want to do: 1-add a member, 2-delete a member, 3-see the arraylist of members, or 4- exit. I have the menu popping up, but I can't get it to go back to the menu after I execute the program once. It needs to keep going until the user enters quit. I've tried everything I can think of. Help please?

public class MemberList
{

    public static void main(String[] args)
    {
      boolean cont = false;
      ArrayList<String> mymembers;
      mymembers = new ArrayList<String>();
      String myS;
      double option;
      myS = JOptionPane.showInputDialog(
        null,
        "Please choose:" +
        "\n 1-Add a new Member." +
        "\n 2-Delete by name."+
        "\n 3-Display All"+
        "\n 4-Quit",
        "Member List",
        JOptionPane.PLAIN_MESSAGE);
      option = Double.parseDouble(myS);
    while (cont) 
    {
        if(option==1)
      {
          String name =
          JOptionPane.showInputDialog(
            null,
            "Please enter a member name to add.",
            "Add",
            JOptionPane.PLAIN_MESSAGE);
          mymembers.add(name);

    }
    else if (option==2)
    {
        String delete =
        JOptionPane.showInputDialog(
         null,
         "Please enter a name to delete.",
         "Delete",
         JOptionPane.PLAIN_MESSAGE);
        mymembers.remove(delete);
        cont=true;
    }
    else if (option==3)
    { 
        int n = mymembers.size();
      for(int i = 0; i < n ; i++)
      System.out.println( mymembers.get( i ) );
      cont=true;
    }
    else if (option==4)
    {

        cont=false;
    }    
} 
  System.exit(0);

}
}  

Recommended Answers

All 2 Replies

You display the menu before entering the while loop,so it only gets displayed once. If you move it to be just inside the while loop it will be displayed every time you go round the loop.

Hello,
What James is suggesting is your answer! I can only add that when you move the the stuff in the while loop you should not forget to set cont to true instead of false. Here is what your code might look like:

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class MemberList
{
    public static void main(String[] args)
    {
      boolean cont = true;
      ArrayList<String> mymembers;
      mymembers = new ArrayList<String>();
      String myS;
      double option;

    while (cont) 
    {
        myS = JOptionPane.showInputDialog(
                null,
                "Please choose:" +
                "\n 1-Add a new Member." +
                "\n 2-Delete by name."+
                "\n 3-Display All"+
                "\n 4-Quit",
                "Member List",
                JOptionPane.PLAIN_MESSAGE);
              option = Double.parseDouble(myS);

        if(option==1)
      {
          String name =
          JOptionPane.showInputDialog(
            null,
            "Please enter a member name to add.",
            "Add",
            JOptionPane.PLAIN_MESSAGE);
          mymembers.add(name);
    }
    else if (option==2)
    {
        String delete =
        JOptionPane.showInputDialog(
         null,
         "Please enter a name to delete.",
         "Delete",
         JOptionPane.PLAIN_MESSAGE);
        mymembers.remove(delete);
        cont=true;
    }
    else if (option==3)
    { 
        int n = mymembers.size();
      for(int i = 0; i < n ; i++)
      System.out.println( mymembers.get( i ) );
      cont=true;
    }
    else if (option==4)
    {
        cont=false;
    }    
} 
  System.exit(0);
}
}  
commented: We don't do peoples homework for them -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.