Looping guestion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    Looping guestion

    I have a problem and I can not fig it out.
    Here is what I need.
    I have scanner when that ask a Question either yes (Y) or no (N).
    if Y is enter then they go to the next level which they can enter a product. This part works ok.
    if N is enter then an a System.out.prin tln("Thank You"). Problem here is either I get a endless loop of Thank you or I get a thank you and then it move to the next level.

    Here is my code.

    Code:
    		// Prompt the user Do you want to create a new product yes or no
    		// make a scanner for the response
    		Scanner kbd_answer = new Scanner(System.in);
    
    		// add loop info here for result Y or N
    		System.out.print("\nPlease press Enter afer each response");
    		System.out.print("\nDo you want to create a Product? (Y/N): ");
    		answer_flag = kbd_answer.next();
    		while (answer_flag.equalsIgnoreCase("N")){
    		System.out.println("Thank You");
    		break;
    		}
    
    		// need to figure out how to close the loop
    
    		// Make a scanner for product
    		Scanner kbd_product = new Scanner(System.in); // scanner called
    														// kbd_product
    		System.out.print("\nEnter the Product Name: ");
    		product_flag = kbd_product.next(); // scanner value is now called
    Any info would be great.
    Thanks
    sandy
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by sandyw
    I have a problem and I can not fig it out.
    Here is what I need.
    I have scanner when that ask a Question either yes (Y) or no (N).
    if Y is enter then they go to the next level which they can enter a product. This part works ok.
    if N is enter then an a System.out.prin tln("Thank You"). Problem here is either I get a endless loop of Thank you or I get a thank you and then it move to the next level.

    Here is my code.

    Code:
    		// Prompt the user Do you want to create a new product yes or no
    		// make a scanner for the response
    		Scanner kbd_answer = new Scanner(System.in);
    
    		// add loop info here for result Y or N
    		System.out.print("\nPlease press Enter afer each response");
    		System.out.print("\nDo you want to create a Product? (Y/N): ");
    		answer_flag = kbd_answer.next();
    		while (answer_flag.equalsIgnoreCase("N")){
    		System.out.println("Thank You");
    		break;
    		}
    
    		// need to figure out how to close the loop
    
    		// Make a scanner for product
    		Scanner kbd_product = new Scanner(System.in); // scanner called
    														// kbd_product
    		System.out.print("\nEnter the Product Name: ");
    		product_flag = kbd_product.next(); // scanner value is now called
    Any info would be great.
    Thanks
    sandy
    Why are you using break? Do you want it to comepletely exit the program? If so, why not use exit(0); (or system.exit(0) or whatever it is)?

    As for the loop, you don't prompt again inside the loop, so the condition never changes (and you never exit the while). Try using an if statement.

    Comment

    • sandyw
      New Member
      • Mar 2007
      • 122

      #3
      Originally posted by sicarie
      Why are you using break? Do you want it to comepletely exit the program? If so, why not use exit(0); (or system.exit(0) or whatever it is)?

      As for the loop, you don't prompt again inside the loop, so the condition never changes (and you never exit the while). Try using an if statement.
      I tried this ...
      Code:
      System.out.print("\nPlease press Enter afer each response");
      		System.out.print("\nDo you want to create a Product? (Y/N): ");
      		answer_flag = kbd_answer.next();
      		if (answer_flag.equalsIgnoreCase("N"))
      
      			System.out.println("Thank You");
      and still get the prompt to go to the next level.
      The program need to end so I will use exit(0) or or system.exit(0) This don't work need to find the right one.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by sandyw
        I tried this ...
        Code:
        System.out.print("\nPlease press Enter afer each response");
        		System.out.print("\nDo you want to create a Product? (Y/N): ");
        		answer_flag = kbd_answer.next();
        		if (answer_flag.equalsIgnoreCase("N"))
        
        			System.out.println("Thank You");
        and still get the prompt to go to the next level.
        The program need to end so I will use exit(0) or or system.exit(0) This don't work need to find the right one.
        It's a System call, so there is a capital 'S'.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          And it might be System.exit(1), but I gave up trying to figure out Java error codes after I attempted to figure out how it handled memory.... (either may work, i don't know - but I'm sure you can play around with it and figure it out)

          Comment

          • sandyw
            New Member
            • Mar 2007
            • 122

            #6
            Opps can do it that way it ends the program even if I type Y...
            I did this.
            Code:
            		System.out.print("\nPlease press Enter afer each response");
            		System.out.print("\nDo you want to create a Product? (Y/N): ");
            		answer_flag = kbd_answer.next();
            		while (answer_flag.equalsIgnoreCase("N")){
            			System.out.println("Thank You");
            		System.exit(1);
            		}
            Seem to work...

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by sandyw
              Opps can do it that way it ends the program even if I type Y...
              I did this.
              Code:
              		System.out.print("\nPlease press Enter afer each response");
              		System.out.print("\nDo you want to create a Product? (Y/N): ");
              		answer_flag = kbd_answer.next();
              		while (answer_flag.equalsIgnoreCase("N")){
              			System.out.println("Thank You");
              		System.exit(1);
              		}
              Seem to work...
              Use

              Code:
               
              System.out.print("\nPlease press Enter afer each response");
              System.out.print("\nDo you want to create a Product? (Y/N): ");
              answer_flag = kbd_answer.next();
              if(answer_flag.equalsIgnoreCase("N")){
                 System.out.println("Thank You");
              	System.exit(0);
              }
              System.exit(0) is for normal program termination and is what you want to use there.

              Comment

              Working...