Results 1 to 6 of 6
  1. #1
    theman279 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Creating a loop help !

    hello !,
    im currently working on a project and having trouble understanding how to return to specific code.


    English Methodology:

    I have a menu which a user can choose different options, however if the user makes a mistake then he will be asked a question: ("would you like to try again")

    Choices: YES or NO


    if user wants to try again type "y"

    if user types "y" = (somehow return to the main menu to restart over)

    if user does not want to try again type "n"

    display ("thank you for trying")


    Here is my code example:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
       public static void  main(String[] args) throws IOException 
    		
     {
      
      System.out.println("what would you like");
      System.out.println("");
      System.out.println("beans or carrots");
    
    
      InputStreamReader input = new InputStreamReader(System.in);
     BufferedReader r = new BufferedReader(input);
    				
    
      String option = r.readLine();
    
      if (option.equalsignorecase ("beans"))
    
        { 
        
              System.out.println("how many beans do you want");
              System.out.println("")
              System.out.println("one")
              System.out.println("two")
              
              String beans = r.readLine();
    
                     if  (beans.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("here is" + beans + bean");          
    
                          }
                     
                      else if (beans.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is" + beans + beans");
    
                           }  
    
                    }
        }
    
      else if (option.equalsignorecase ("carrots"))
    
           {
              System.out.println("how many carrots do you want");
              System.out.println("")
              System.out.println("one")
              System.out.println("two")
              
              String carrots = r.readLine();
    
                     if  (carrots.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("here is" + carrots + carrot");          
    
                          }
                     
                      else if (carrots.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is" + carrots + carrot");
    
                           }  
    
                    }
    
          else
    
             { 
    	System.out.println("Invalid entry has occured, would you like to try again? Enter Y or N");
    
            String error = r.readline();
    
                if (error.equalsIgnorecase  ("Y")
                    
                          {  //  ***** RETURN TO START ??? *****}
    
                 if (error.equalsIgnorecase  ("N")
                    
                          { 
                           System.out.println("Thank you for trying"); 
                          }       
    
         }
    =============

    This was a quick mock up , for example purposes so there might be errors in it , but its to give a example of what im trying to do :)


    All i need is to return to the start of the program when the user enters "Y" , can't figure it out for the life of me ! if you could enlighten me that would be great !

    Thanks !
    Last edited by theman279; 03-08-2011 at 06:02 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    User a do-while loop. So user have to go through at lease once through the menu and if user makes a mistake you can prompt the question to asking retry. That user input validate as the loop condition.

  3. #3
    theman279 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Ah do-while loops :S , i understand the concept but integrating it with if statements or long code i get lost lol ! i would expect im trying to return
    to the beginning of the code so "do" must be at the start to repeat
    however im not sure how to properly integrate the while inside if statements.
    it seems to throw errors left,right and center at me!



    ***** UPDATE ***** [SOLVED] -- Managed to get the do while to return to start !!!!! >>> if anyone wants some code like this ? your welcome to use this as an example!

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class foodtest  {
    	
    	
    	public static String error = "";
    	
    	
       public static void  main(String[] args) throws IOException 
    		
       
       {
    	   
    	  
      do {
      System.out.println("what would you like?");
      System.out.println("");
      System.out.println("beans or carrots");
    
    
      InputStreamReader input = new InputStreamReader(System.in);
     BufferedReader r = new BufferedReader(input);
    				
    
      String option = r.readLine();
    
      if (option.equalsIgnoreCase ("beans"))
    
        { 
        
              System.out.println("how many beans do you want?");
              System.out.println("");
              System.out.println("one");
              System.out.println("two");
              
              String beans = r.readLine();
    
                     if  (beans.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("there is " + beans + " bean");
                          }
                     
                      else if (beans.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is " + beans + " beans");
    
                           }  
    
                    }
        
    
      else if (option.equalsIgnoreCase ("carrots"))
    
           {
              System.out.println("how many carrots do you want?");
              System.out.println("");
              System.out.println("one");
              System.out.println("two");
              
              String carrots = r.readLine();
    
                     if  (carrots.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("here is " + carrots + " carrot");          
    
                          }
                     
                      else if (carrots.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is " + carrots + " carrot");
    
                           }  
    
                    }
                
              
      		
    
      		
          else
    
             { 
        	  	System.out.println("Invalid entry has occured, would you like to try again? Enter Y or N");
        	  	error = r.readLine();
            
        	  
             // }  **i thought do would end here, but this will just end the else**
                if (error.equalsIgnoreCase  ("Y"))
                	 
                          {  
                	
                			//  ***** RETURN TO START ??? *****}
                	
                	
                           } 
                 if (error.equalsIgnoreCase  ("N"))
                    
                          { 
                           System.out.println("Thank you for trying"); 
                          }       
           
             }           
               } // END DO
       
             while (error.equalsIgnoreCase("Y"));
             
            	
             }
            }
    Last edited by theman279; 03-09-2011 at 07:11 PM. Reason: [SOLVED]

  4. #4
    theman279 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default do while loop - inside a do - while which will take effect in a If statement

    Hey , i know i solved the previous problem but another problem emerges with running a do while inside a do while inside a if statement. let me make more sense of this for u.

    do the program , while the error is non existant.

    if user does not want to repeat code , then return to beginning

    Here is a code representation of my problem.

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class foodtest  {
    	
    	
    	public static String error = "";
                public static String returning = "";
    
    	
    	
       public static void  main(String[] args) throws IOException 
    		
       
       {
    	   
    	  
      do {
      System.out.println("what would you like?");
      System.out.println("");
      System.out.println("beans or carrots");
    
    
      InputStreamReader input = new InputStreamReader(System.in);
     BufferedReader r = new BufferedReader(input);
    				
    
      String option = r.readLine();
    
      if (option.equalsIgnoreCase ("beans"))
    
        { 
        
              System.out.println("how many beans do you want?");
              System.out.println("");
              System.out.println("one");
              System.out.println("two");
              
              String beans = r.readLine();
    
                     if  (beans.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("there is " + beans + " bean");
                          }
                     
                      else if (beans.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is " + beans + " beans");
    
                           }  
    
                    }
        
    
      else if (option.equalsIgnoreCase ("carrots"))
    
           {
              System.out.println("how many carrots do you want?");
              System.out.println("");
              System.out.println("one");
              System.out.println("two");
              
              String carrots = r.readLine();
    
                     if  (carrots.equalsIgnoreCase ("one"))
                        
                         { 
                           System.out.println("here is " + carrots + " carrot");          
    
                          }
                     
                      else if (carrots.equalsIgnoreCase ("two"))
    
                           {
                            
                            System.out.println("here is " + carrots + " carrot");
                           System.out.println("");
    
          //*******HERE IS THE PROBLEM**************
    
                            
    
                           System.out.println("would you like to return to the start. Enter Y or N");   
                          returning = r.readLine();
                          
    
                                        if (return.equalsIgnoreCase("Y)")
    
                                               {   
                                                        // HERE I NEED TO GET BACK TO THE START
                                                   
    
                                               }
    
                                       if (return.equalsIgnoreCase("N)")
    
                                               {   
                                                       System.out.println("Thank you for trying.");
                                                   
    
                                               }
    
                          }  
    
                    }
                
              
      		
    
      		
          else
    
             { 
        	  	System.out.println("Invalid entry has occured, would you like to try again? Enter Y or N");
        	  	error = r.readLine();
            
        	  
             // }  **i thought do would end here, but this will just end the else**
                if (error.equalsIgnoreCase  ("Y"))
                	 
                          {  
                	
                			//  ***** RETURN TO START ??? *****}
                	
                	
                           } 
                 if (error.equalsIgnoreCase  ("N"))
                    
                          { 
                           System.out.println("Thank you for trying"); 
                          }       
           
             }           
               } // END DO
       
             while (error.equalsIgnoreCase("Y"));
             
            	
             }
            }

    Not sure how to put a do - while inside a if statement with a do - while already covering the code!

    Help would be appreciated :) ! thanks
    Last edited by theman279; 03-09-2011 at 09:02 PM. Reason: small mistake = public static string return should be "string returning

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Is that your code really compiled?

    Java Code:
    returning = r.readLine();
                          
    
                                        if (return.equalsIgnoreCase("Y)")

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Go through my comments.

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class foodtest {
        public static String error = "";
        public static String returning = "";
    
        public static void main(String[] args) throws IOException {
    
            do {
                System.out.println("what would you like?");
                System.out.println("");
                System.out.println("beans or carrots");
    
                InputStreamReader input = new InputStreamReader(System.in);
                BufferedReader r = new BufferedReader(input);
    
                String option = r.readLine();
    
                if (option.equalsIgnoreCase("beans")) {
    
                    System.out.println("how many beans do you want?");
                    System.out.println("");
                    System.out.println("one");
                    System.out.println("two");
    
                    String beans = r.readLine();
    
                    if (beans.equalsIgnoreCase("one")) {
                        System.out.println("there is " + beans + " bean");
                    }
                    else if (beans.equalsIgnoreCase("two")) {
                        System.out.println("here is " + beans + " beans");
                    }
                }
                else if (option.equalsIgnoreCase("carrots")) {
                    System.out.println("how many carrots do you want?");
                    System.out.println("");
                    System.out.println("one");
                    System.out.println("two");
    
                    String carrots = r.readLine();
    
                    if (carrots.equalsIgnoreCase("one")) {
                        System.out.println("here is " + carrots + " carrot");
    
                    } else if (carrots.equalsIgnoreCase("two")) {
                        System.out.println("here is " + carrots + " carrot");
                    }
    
                }
                // This is the error you comes with. At this time set the value of variable 'error'
                else {
                    System.out.println("Invalid entry has occured, would you like to try again? Enter Y or N");
                    error = r.readLine();
                }
            } // END DO
              // Their is nothing really called END DO. It's do-while loop.
            while (error.equalsIgnoreCase("Y"));
        }
    }

Similar Threads

  1. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 03:37 AM
  2. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 09:00 PM
  3. Creating a Loop
    By SenorJalapeno in forum New To Java
    Replies: 3
    Last Post: 04-02-2010, 10:13 AM
  4. Replies: 1
    Last Post: 07-22-2009, 10:10 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •