I have problem with do while loop

I need something like this

What do you want to do?
        1  Create a new account
        2  Deposite funds
        3  Withdraw funds
        4  Show Balance
        5  Show Interest
        6  Show Summary
        7  Quit
Enter Option: 2


Which account <1 to 2> do you wish to use?  2000
ERROR Please Enter an account number between 1 and 2

Which account <1 to 2> do you wish to use? 2

How much money would you like to deposit?

but it just stops at the error message?

public static void input2() // option 2
      {
      
      // input Scanner
         Scanner in = new Scanner(System.in);
      
         int input1 = 0;
         boolean check2 = false;
      
         do {
         
         
            if (account.size() == 1) {
               System.out.print("\nHow much money would you like to deposit? ");
               
            
               input1 = in.nextInt();
            
               account.get(0).deposit(input1);
					check2 = true;
            } 
            else if (account.size() > 1) {
               System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");
               
            
               input1 = in.nextInt();
					boolean check3 = true;
            	do {
               if (input1 <= account.size())
               {
               
               
                  System.out.print("\nHow much money would you like to deposit? ");
                  int take = 0;
               
                  take = in.nextInt();
               
               
                  account.get(input1 - 1).deposit(take);
						
               }
               else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
                  check3 = false;
               
               } } while(!true);
               
            }
             
          
         
         
         } while (!true);
      
      } // end option2

I have problem with do while loop

I need something like this

What do you want to do?
        1  Create a new account
        2  Deposite funds
        3  Withdraw funds
        4  Show Balance
        5  Show Interest
        6  Show Summary
        7  Quit
Enter Option: 2


Which account <1 to 2> do you wish to use?  2000
ERROR Please Enter an account number between 1 and 2

Which account <1 to 2> do you wish to use? 2

How much money would you like to deposit?

but it just stops at the error message?

public static void input2() // option 2
      {
      
      // input Scanner
         Scanner in = new Scanner(System.in);
      
         int input1 = 0;
         boolean check2 = false;
      
         do {
         
         
            if (account.size() == 1) {
               System.out.print("\nHow much money would you like to deposit? ");
               
            
               input1 = in.nextInt();
            
               account.get(0).deposit(input1);
					check2 = true;
            } 
            else if (account.size() > 1) {
               System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");
               
            
               input1 = in.nextInt();
					boolean check3 = true;
            	do {
               if (input1 <= account.size())
               {
               
               
                  System.out.print("\nHow much money would you like to deposit? ");
                  int take = 0;
               
                  take = in.nextInt();
               
               
                  account.get(input1 - 1).deposit(take);
						
               }
               else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
                  check3 = false;
               
               } } while(!true);
               
            }
             
          
         
         
         } while (!true);
      
      } // end option2

im not sure but your do while looks suspicious shouldnt you be using:

do {
do {
if() {
check2=true;
check3=true;
} else {
}
}while(!check3)//while check3 is not true
} while(!check2)//while check2 is not true

also declare your booleans outside the loops and set them to flase, and declare them only true in the most inner loop because if you declare check2 as true, your outer do while will not carry on looping and thus neither will your inner only once which when error occurs means it will stop. i'd suggest making 2 seperate do whiles

Edited 4 Years Ago by DavidKroukamp: n/a

also your "System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");" is not in the loop...

but definitly "} while(!true);" is asking for problems if you don't have a "break;" statement

boolean continue = true;

do{
    //ask a question

    //get the answer

    if(answer is valid){
        //do stuff you need to do to move on

        //set continue to false
        continue = false;
    }
    else{
        //tell the user he messed up
    }
}while(continue);

Edited 4 Years Ago by Philippe.Lahaie: n/a

then I get infinite loop

when you use which method mine or Philippe.Lahaie? you might want to use a break statement when you've got what you want then?

break;

or you might try adding this ' input1 = in.nextInt();' to your inner most loop...... so that is re asks for input

Edited 4 Years Ago by DavidKroukamp: n/a

again I have problems

public static void input2() // option 2
      {
      
      // input Scanner
         Scanner in = new Scanner(System.in);
      
         int input1 = 0;
         boolean check2 = false;
		//	boolean check3 = true;
      
         do {
         
         
            if (account.size() == 1) {
               System.out.print("\nHow much money would you like to deposit? ");
               
            
               input1 = in.nextInt();
            
               account.get(0).deposit(input1);
					check2 = false;
            } 
            else if (account.size() > 1) {
              
				System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");
               
            
               input1 = in.nextInt();
					
            	
               if (input1 <= account.size())
               {
               
               
                  System.out.print("\nHow much money would you like to deposit? ");
                  int take = 0;
               
                  take = in.nextInt();
               
               
                  account.get(input1 - 1).deposit(take);
						check2 = false;
						
               }
               else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
                  check2 = true;
               
               } 
               
            }
             
          
         
         
         } while (!true);
      
      } // end option2

again I have problems

public static void input2() // option 2
      {
      
      // input Scanner
         Scanner in = new Scanner(System.in);
      
         int input1 = 0;
         boolean check2 = false;
		//	boolean check3 = true;
      
         do {
         
         
            if (account.size() == 1) {
               System.out.print("\nHow much money would you like to deposit? ");
               
            
               input1 = in.nextInt();
            
               account.get(0).deposit(input1);
					check2 = false;
            } 
            else if (account.size() > 1) {
              
				System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");
               
            
               input1 = in.nextInt();
					
            	
               if (input1 <= account.size())
               {
               
               
                  System.out.print("\nHow much money would you like to deposit? ");
                  int take = 0;
               
                  take = in.nextInt();
               
               
                  account.get(input1 - 1).deposit(take);
						check2 = false;
						
               }
               else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
                  check2 = true;
               
               } 
               
            }
             
          
         
         
         } while (!true);
      
      } // end option2

what are the problems i cant run the code you gave
but why are you still using this

} while (!true);

?
please see above as we advised you

Edited 4 Years Ago by DavidKroukamp: n/a

with

while (!check2) i get infinite loop

uncomment here:

else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
                //  check2 = true;
 
               }

and now use :

}while(check2==true)

if problem persists why not make a

while(check2=true) {

 
            if (account.size() == 1) {
               System.out.print("\nHow much money would you like to deposit? ");
 
 
               input1 = in.nextInt();
 
               account.get(0).deposit(input1);
					check2 = false;
            } 
            else if (account.size() > 1) {
 
				System.out.print("\nWhich account <1 to " + account.size() + "> do you wish to use? ");
 
 
               input1 = in.nextInt();
 
 
               if (input1 <= account.size())
               {
 
 
                  System.out.print("\nHow much money would you like to deposit? ");
                  int take = 0;
 
                  take = in.nextInt();
 
 
                  account.get(input1 - 1).deposit(take);
						check2 = false;
 
               }
               else
               {
                  System.out.print("\nERROR Please Enter an account number between 1 and " + account.size());
                  System.out.println();
 
               } 
 
            }
}

instead of do while which will make it ask the user an extra time if no break statement is used

Edited 4 Years Ago by DavidKroukamp: n/a

again I get infinite loop

I don get this :(

i can see another reason why an infinite loop is caused try this logic: add a simple counter which increments by one once the first if statement is taken so the next time you loop checking for the error we do not ask again for deposit amount etc:

int count=0;
while(check2==true){
if(counter!=1) {//so we dont ask twice
counter++;
...
//ask for deposit amount account size
}//end of count!=1
if () {//no more else if check account size here if correct will ask for other input
...//ask for input
if () {//if its correc
check2=false;
} else {//error message
} //end of checking input if statement
else {//account size was wrong
counter--;
}//end of account size check if statemnt
}//end of while loop

Edited 4 Years Ago by DavidKroukamp: n/a

you took out the inner loop....

you want multiple loops, 1 to come back to the menu after every action , and 1 for each action/validation depending on how you want you validation to work.

//start main

do{
    //print menu

    //get user input from menu

    //switch case on user input

        //case "deposit" :


boolean continue = true;

do{
    //ask a question

    //get the answer

    if(answer is valid){
        //do stuff you need to do to move on

        //set continue to false
        continue = false;
    }
    else{
        //tell the user he messed up
    }
}while(continue);

        break;
    //end case
while(userinput != exitcode);

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.