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 2 of 2

Thread: Help with some loop in java

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with some loop in java

    Hi, I'm a beginner in java I try to write a while loop how I can choose when I want to leave It, I write that :

    int z=0;
    client c1= new client("roger");
    labelK:while(z!=1) {

    System.out.print("Votre nom et v?tre pr?nom? \n");
    String st=sc.nextLine();
    c1.setclient(st);

    System.out.println("Voulez-vous continuer ?");
    int rts = sc.nextInt();

    if(rts !=1) {
    break labelK;
    }
    }
    System.out.println("Eureka");
    z=1;
    My probleme is when I want to remake a loop (when I have to write rts=1), it skip automaticly String st=sc.nextLine(); and I don't know why the second time is different from the first one

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with some loop in java

    Can you copy the program's output from the command prompt window and paste it here so we can see what you are talking about?
    Also add some comments to the print out describing the problem: <<<<< Here it should ...

    The loop has two variables that are used to control its looping: z and rts
    That is confusing. Can you change the logic so that only 1 variable is used?


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.


    it skip automaticly String
    There is a problem with using nextInt followed by nextLine. nextInt leaves the line-end character in the Scanner's buffer. nextLine will return that line-end character as an empty line.
    The solution is to call nextLine after calling nextInt to clear the line-end character.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JAVA LOOP HELP!!!
    By sanjam58 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 16th, 2012, 05:49 PM
  2. loop in java
    By casperl90 in forum Loops & Control Statements
    Replies: 2
    Last Post: August 8th, 2011, 03:17 AM
  3. loop in java
    By jonnitwo in forum Loops & Control Statements
    Replies: 4
    Last Post: July 8th, 2011, 02:13 PM
  4. for loop in java
    By kissmiss in forum Loops & Control Statements
    Replies: 1
    Last Post: December 16th, 2009, 03:47 AM
  5. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM