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

Thread: while loop

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while loop

    Hello there
    I am beginning in java, and can't figure out what is wrong with my code

    Right now I am learning about loops
    and I got stuck in assignment name " running the race "

    assignment is following

    write the program that asks for the names of 3 runners, and the time in minutes it took each of them to finish the race. the program should display names of the runners in the order.


    problem is:

    when I run program. First it goes right asks me name of runner and then time
    and, then when it goes second time on loop instead of asking me name of the runner first and then time it does both simultaneously

    example:

    Enter runner's name
    Leo
    Enter runner's time
    44
    Enter runner's name
    Enter runner's time


    Code is bellow:

     
    import java.util.Scanner;
     
    class Main {
     
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
     
        String name;
        double time ;
     
     
        int runner = 0;
        while( runner < 3)
        {
          System.out.println("Enter runner's name ");
          name = sc.nextLine();
     
          System.out.println("Enter  runner's time ");
          time = sc.nextDouble();
     
          runner ++ ;
     
     
     
     
        }
     
     
     
     
      }
     
    }
    Last edited by moraleslevan@gmail.com; October 18th, 2020 at 11:30 AM.

  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: while loop

    The problem is the way the Scanner handles line ends. Bascially the code needs an extra nextLine to clear Scanner's buffer.
    Read this for more info: https://www.geeksforgeeks.org/why-is...ext-functions/

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

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

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop

    OH wow I just fixed

    I just remove " Line "
    so its like
    name = sc.next();

    so it works now


    thank you so much

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. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM