Do While loop in menu

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4680 Views - Last Post: 19 February 2011 - 12:49 PM Rate Topic: -----

#1 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Do While loop in menu

Posted 17 February 2011 - 10:07 AM

I am working on a school assignment and I am creating a Bank System. In the menu, the user can choose his option and so far everything is fine but i need to include a loop so that the user can choose another option if mistaken. Any ideas please of what kind of loop and where do i have to put it? This is the program so far.
import javax.swing.JOptionPane;
   public class Menu{
       public static void main(String [] args){
           System.out.println("\t*MENU");
           System.out.println("0. Exit");
           System.out.println("1. Add New Entry");
           System.out.println("2. Edit Entry");
           System.out.println("3. Display list of Clients");
           System.out.println("4. Delete entry");
           System.out.println("5. Deposit");
           System.out.println("6. Withdrawal");
           String opt = JOptionPane.showInputDialog("Enter option");
           int option = Integer.parseInt(opt);
           switch(option) {
               case 0 : System.out.println("Exiting"); break;
               case 1 : System.out.println("Adding New Entry"); break;
               case 2 : System.out.println("Editing Entry"); break;
               case 3 : System.out.println("Displaying list of Clients"); break;
               case 4 : System.out.println("Deleting Entry"); break ;
               case 5 : System.out.println("Depositing"); break;
               case 6 : System.out.println("Withrawing"); break;
               default: System.out.println("Invalid Option"); break;
            }
        }  
}


This post has been edited by japanir: 17 February 2011 - 10:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Do While loop in menu

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Do While loop in menu

Posted 17 February 2011 - 10:17 AM

A while loop would be better in that case.
you can loop while the user's input is bigger than 6 OR smaller than 0.
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Do While loop in menu

Posted 17 February 2011 - 10:23 AM

Do you mean you need the user to confirm the choice made was the correct choice?

Before entering your switch, you could ask, "Are you sure you want to <blank> Y/N? " with the blank filled in with what the user chose. If 'Y' (the default choice), continue with the switch logic, if 'N', present the menu to the user again to get the correct choice.

How to present the menu again? You could put it in a do-while loop and add logic that keeps the menu/switch going until the user chooses to exit.
Was This Post Helpful? 1
  • +
  • -

#4 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Re: Do While loop in menu

Posted 17 February 2011 - 11:02 AM

Sorry to interrupt again, but i did the while loop and now I have another error, maybe you can help me please. I did the following program, and the error is java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at Menu.main(Menu.java:13)


import javax.swing.JOptionPane;
   public class Menu{
       public static void main(String [] args){
           System.out.println("\t*MENU");
           System.out.println("0. Exit");
           System.out.println("1. Add New Entry");
           System.out.println("2. Edit Entry");
           System.out.println("3. Display list of Clients");
           System.out.println("4. Delete entry");
           System.out.println("5. Deposit");
           System.out.println("6. Withdrawal");
           String opt = JOptionPane.showInputDialog("Enter option");
           int option = Integer.parseInt(opt);
           switch(option) {
               case 0 : System.out.println("Exiting"); break;
               case 1 : System.out.println("Adding New Entry"); break;
               case 2 : System.out.println("Editing Entry"); break;
               case 3 : System.out.println("Displaying list of Clients"); break;
               case 4 : System.out.println("Deleting Entry"); break ;
               case 5 : System.out.println("Depositing"); break;
               case 6 : System.out.println("Withrawing"); break;
               default: System.out.println("Invalid Option"); break;
            }
            int i=0;
            int j=6;
               while(i>6)
               {
               System.out.println("Option is not available");
           while(j<0)
               {
               System.out.println("Option is not available");
            }    
        }
    }
}


EDIT: Post your code inside code tags

This post has been edited by japanir: 17 February 2011 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Do While loop in menu

Posted 17 February 2011 - 11:17 AM

The loop to validate the input should be right after you get user's input.
you should check the value of option, like:
while(option < 0 || option > 6){
  bad input..
  get new user input
}


As for the Exception, did you close the JDialogue before inputting anything?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Do While loop in menu

Posted 17 February 2011 - 08:01 PM

import javax.swing.JOptionPane;
   public class Menu{
       public static void main(String [] args){
         for(;;)/> {      // loop for ever
           System.out.println("\t*MENU");
           System.out.println("0. Exit");
           System.out.println("1. Add New Entry");
           System.out.println("2. Edit Entry");
           System.out.println("3. Display list of Clients");
           System.out.println("4. Delete entry");
           System.out.println("5. Deposit");
           System.out.println("6. Withdrawal");
           String opt = JOptionPane.showInputDialog("Enter option");
           int option = Integer.parseInt(opt);
           switch(option) {
               case 0 : System.out.println("Exiting"); return;    // <---    exiting
               case 1 : System.out.println("Adding New Entry"); break;
               case 2 : System.out.println("Editing Entry"); break;
               case 3 : System.out.println("Displaying list of Clients"); break;
               case 4 : System.out.println("Deleting Entry"); break ;
               case 5 : System.out.println("Depositing"); break;
               case 6 : System.out.println("Withrawing"); break;
               default: System.out.println("Invalid Option"); break;
            }
          }    // <--- en for(;;)/>
        }  
}


This post has been edited by pbl: 17 February 2011 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#7 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Re: Do While loop in menu

Posted 18 February 2011 - 08:34 AM

I am doing a school assignment for school regarding Bank Systems. In the following program, there's a menu and the user has to choose his needed destination. I need to use the Do While loop so that the program loops till the user presses 0 (to exit) Any ideas please because I'm stuck :/ ... This is the program
import javax.swing.JOptionPane;
   public class Menu{
       public static void main(String [] args){
           System.out.println("\t*MENU");
           System.out.println("0. Exit");
           System.out.println("1. Add New Entry");
           System.out.println("2. Edit Entry");
           System.out.println("3. Display list of Clients");
           System.out.println("4. Delete entry");
           System.out.println("5. Deposit");
           System.out.println("6. Withdrawal");
           String opt = JOptionPane.showInputDialog("Enter option");
           int option = Integer.parseInt(opt);
           switch(option) {
               case 0 : System.out.println("Exiting"); break;
               case 1 : System.out.println("Adding New Entry"); break;
               case 2 : System.out.println("Editing Entry"); break;
               case 3 : System.out.println("Displaying list of Clients"); break;
               case 4 : System.out.println("Deleting Entry"); break ;
               case 5 : System.out.println("Depositing"); break;
               case 6 : System.out.println("Withrawing"); break;
               default: System.out.println("Invalid Option"); break;
            }
            do {

This post has been edited by japanir: 18 February 2011 - 08:40 AM
Reason for edit:: code tags added

Was This Post Helpful? 0
  • +
  • -

#8 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Do While loop in menu

Posted 18 February 2011 - 08:37 AM

At which point are you stuck? Do you not know how a do-while-loop works? Or does your code create any errors?
Please, be more specific and I'd be glad to help.
Was This Post Helpful? 0
  • +
  • -

#9 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Re: Do While loop in menu

Posted 18 February 2011 - 08:40 AM

I dont have an idea of what can i write in a do while loop cause i never used it before as i am still a beginner. Yes, till now the program runs perfectly but i need to add a do while loop so that the program loops till the user decides to exit the program. And thanx for your help :)
Was This Post Helpful? 0
  • +
  • -

#10 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Do While loop in menu

Posted 18 February 2011 - 08:45 AM

Basically, a do-while loop is the same as a while loop, with the difference, that the code inside the loop is at least run once. The while loop checks the condition before it runs the code in its body, the do-while checks afterwards.
do {
  //the code that is meant to be run in the loop
} while (/*your condition*/)



In you case maybe something like:
do {
  //display menu entries 
  //and perform operations
} while (0 != usersChoice)


Was This Post Helpful? 1
  • +
  • -

#11 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Do While loop in menu

Posted 18 February 2011 - 08:46 AM

Duplicate topics merged.
Was This Post Helpful? 0
  • +
  • -

#12 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Re: Do While loop in menu

Posted 18 February 2011 - 09:01 AM

Now i have another problem regarding variables and initializing variables. If i declare Menu and option as variables int, how can i declare them please?

int Menu = ?;
int option = ?;

Sorry if I'm asking a lot of silly questions, but I'm still a beginner :/
Was This Post Helpful? 0
  • +
  • -

#13 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Do While loop in menu

Posted 18 February 2011 - 09:10 AM

That is a proper way to declare an int variable called option:
int option;

if declared as a global variable the compiler assigns it the default value 0.
However, if declared as a local variable, you MUST assign it some value before using it.
you can assign a value on the declaration line:
int option = 1;

or, you can assign a value in a seperate line:
int option;
option = 1;

Was This Post Helpful? 0
  • +
  • -

#14 Janice018  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-February 11

Re: Do While loop in menu

Posted 18 February 2011 - 09:21 AM

hmm i see .. so when initializing a variable, only numbers are used? If so how can i initialize Menu please?
Was This Post Helpful? 0
  • +
  • -

#15 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Do While loop in menu

Posted 18 February 2011 - 09:33 AM

It depends of the variable's data type.
global variables are getting the default value from the compiler:
double is assigned 0.0
int is assigned 0
String is assigned null
etc.

Pay attention when you say numbers.
a number can be an int, and accept integer values like 2, 4, 5, -2 etc
a number can also be a double value and accept real values, like: 2.4222, 4.0 -0.5 etc.

As for your question how do you want to use int Menu?
Note that Menu is also the name of your class. it is not a good idea to declare a variable with the same name as a class.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2