Results 1 to 3 of 3
  1. #1
    RockW is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Loop returns at second input rather than first.

    Could someone provide some input on why my loop is returning back to the second portion of the program? I would actually prefer that the first loop keep looping until "stop" is issued but at this point I will take any advice to have it start at step 1.


    import java.util.Scanner;

    public class Payrollb
    {
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );


    int dept1;
    double salary1;
    double sum;


    boolean contRun = true;
    while (contRun)
    {

    System.out.println( "Enter Name of The Department:" );
    String deptName = input.nextLine();
    if(deptName.equalsIgnoreCase("stop")){


    }



    do
    {


    System.out.println("Enter The Number of Employees In The Department:" );
    dept1 = input.nextInt();
    if(dept1 < 0)
    System.out.println("Number Must Be A Positive");
    }
    while(dept1 < 0);


    do
    {

    System.out.println("Enter The Average Salary Of Employees:" );
    salary1 = input.nextDouble();
    if(salary1 < 0)
    System.out.println("Number Must Be A Positive");
    }
    while(salary1 < 0);

    sum = dept1 * salary1;

    System.out.println( "For Department: " + deptName);
    System.out.println( "Average Salary Per Employee Is: $" + sum );


    }

    }
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    You're missing a } in your first loop, causing the other loops to be part of that loop.

    Also, if you didn't already know, "break;' terminates the loop so you can use that when the user enters stop.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    OP,

    Use Code Tags while posting your code. And read about the branching statements in Java : Branching Statements

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Every Input File I Use Returns "Unsorted"
    By Cod in forum New To Java
    Replies: 36
    Last Post: 02-27-2011, 07:43 AM
  2. while loop bypasses scanner input on 2nd pass
    By xf021209 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 09:10 AM
  3. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 11:02 PM
  4. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 11:02 PM
  5. Perfect Square Array Input Using For Loop
    By dalangley in forum New To Java
    Replies: 9
    Last Post: 01-27-2009, 02:33 AM

Posting Permissions

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