Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Loop into another loop

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Loop into another loop

    Hello I'm trying to do something -when the user has entered a value other than 1 or 2, that it prompts the user to try again until the user chooses 1 or 2 - once the user has been chosen 1 or 2 make another loop that will go back to the very beginning Here is the code I entered

     package Com.mkl;
     
    import java.util.Scanner;
     
    public class Salut{
     
    	public static void main(String[] args) {
         Scanner lectureClavier=new Scanner(System.in);
         int str=0;
         char r=' ';
    		do {
    		do {
    		  System.out.println("choisi 1 ou 2");
    		  str=lectureClavier.nextInt();
    		System.out.println("ressayee");
    		 } while(str!= 1 && str!=2);
    		 if (str==1 || str==2){
    		  System.out.println("correct");
    		  }
    		 r= lectureClavier.nextLine().charAt(0);
    		 System.out.println("aimerais vous revenir au début ? tapé O ou N");
          } while(r=='N') ;
     
    		 }
     
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop into another loop

    Can you explain what problems you are having? What happens when you compile and execute the code?

    One problem I see is using the Scanner class's nextLine method after using the nextInt method.
    The next... method leaves the endOfLine character in the Scanner's buffer. nextLine then reads that as an empty line.
    It is explained here:
    https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop into another loop

    The second loop (do while) does not work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop into another loop

    The second loop (do while) does not work
    Is that the loop that tests the value in r?
    What is the value in r at the end of the loop just before the while statement tests it?
    print the value of r so you can see its value:
      System.out.println("r="+r+"<");   // show r's value

    Did you try to fix the problem I pointed out with the Scanner class's next... methods?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM