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: Help with my loop

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my loop

    Hi I am currenlty taking my first Java class and I am having a little trouble. My current assignment involves modifying my payroll program application so it continues to request employee information until the user enters stop as the employee name. I think I am on the right track but I am confused how to get the loop I have to prompt for a name each time as well as the payrate and hours worked. Any suggestions would be greatly appreciated.

    // Payroll1.java
    // Payroll1 that displays calculates weekly pay for an employee
    import java.util.Scanner; // program uses class Scanner
     
    public class Payroll1
    {
       // main method begins execution of Java application
       public static void main( String args[] )
       {
          // create Scanner to obtain input from command window
          Scanner input = new Scanner( System.in );
     
          String name; // employees name
          int payrate; // employees hourly rate
          int hours; // hours employee worked for the week
          int product; // product of payrate and hours
     
          System.out.print( "Enter Employee's name: , enter stop to end." ); // prompt
          name = input.nextLine(); // read name from user
     
          while ( name != ("stop") )
          {
             System.out.print( "Enter payrate: " ); // prompt
             payrate = input.nextInt(); // read hourly rate from user
             while ( payrate <= 0 )
             {
                System.out.print( "Payrate must be a positive number, try again: $" );
                payrate = input.nextInt();
             }
     
             System.out.print( "Enter hours worked: " ); // prompt
             hours = input.nextInt(); // read hours worked from user
             while ( hours <= 0 )
             {
                System.out.print( "Hours must be a positive number, try again: " );
                hours = input.nextInt();
             }
     
     
             product = payrate * hours; // multiply payrate times hours worked
     
             System.out.printf( "Employee Name: %s, Weekly pay is $%d%n", name, product );
          } // end while
     
       } // end method main
     
    } // end class Payroll1


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with my loop

    Hello Xrrak,

    Welcome to the Java Programming Forums.

    Here is a quick code snippet for you which may help:

    import java.util.*;
     
    public class Test {
     
        public static void main(String args[]) {
     
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter your name: ");
     
            while (sc.hasNext()) {
                String name = sc.next();
     
                if (name.equals("stop")) {
                    System.exit(0);
                }
     
                System.out.println("Hello " + name);
                System.out.println("Enter your name: ");
            }
     
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with my loop

    I have modified your original code using the example I gave above. You can see the changes in bold.

    This should solve your problem

    package test;
     
    //Payroll1.java
    //Payroll1 that displays calculates weekly pay for an employee
    import java.util.Scanner; // program uses class Scanner
     
    public class Payroll1
    {
    // main method begins execution of Java application
    public static void main( String args[] )
    {
       // create Scanner to obtain input from command window
       Scanner input = new Scanner( System.in );
     
       String name; // employees name
       int payrate; // employees hourly rate
       int hours; // hours employee worked for the week
       int product; // product of payrate and hours
     
       System.out.print( "Enter Employee's name: , enter stop to end." ); // prompt
     
       [B]while (input.hasNext())[/B]
       {       
          name = input.next(); // read name from user
     
          [B]if (name.equals("stop")) {
              System.exit(0);
          }[/B]
     
          System.out.print( "Enter payrate: " ); // prompt
          payrate = input.nextInt(); // read hourly rate from user
          while ( payrate <= 0 )
          {
             System.out.print( "Payrate must be a positive number, try again: $" );
             payrate = input.nextInt();
          }
     
          System.out.print( "Enter hours worked: " ); // prompt
          hours = input.nextInt(); // read hours worked from user
          while ( hours <= 0 )
          {
             System.out.print( "Hours must be a positive number, try again: " );
             hours = input.nextInt();
          }
     
     
          product = payrate * hours; // multiply payrate times hours worked
     
          System.out.printf( "Employee Name: %s, Weekly pay is $%d%n", name, product );
     
          [B]System.out.print( "Enter Employee's name: , enter stop to end." ); // prompt[/B]
       } // end while
     
    } // end method main
     
    } // end class Payroll1
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. 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, 04:27 PM
  3. Do While Loop
    By connex in forum Loops & Control Statements
    Replies: 6
    Last Post: December 7th, 2009, 02:54 PM
  4. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 01:10 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM