Your Ad Here Your Ad Here
Results 1 to 3 of 3

Thread: why doesn't this work (java)

  1. #1
    bornwithnoname's Avatar Bit Master BT Rep: +1
    Join Date
    Sep 2006
    Location
    USA
    Posts
    368
    I am going to post some code.. If someone could explain why it doesn't work the way I think it should it would be helpful. My comments will be in blue.

    Code:
    
    
    import java.util.Scanner; // program uses Scanner
    
    
    public class WeekTwoCheckPoint {
    
    
        public static void main(String[] args)
    
        {
    
    
            Scanner input = new Scanner( System.in );
    
            float PayRate = 0;
            float HoursWorked = 0;
            float GrossPay;
            String EmployeeName = new String ("");
    
    
    
    
            while (!EmployeeName.equalsIgnoreCase ("stop")) At this point I have established the program should only loop if EmployeeName does not equal "stop" What happens is if I enter stop the loop runs once then stops. Why?
    
                { // open loop
    
            System.out.println( "Please enter employee name or stop to quit:" ); // input employee name
            EmployeeName = input.nextLine();
    
            System.out.println( "Please enter employee pay rate:" ); //input emplyee rate
            PayRate = input.nextFloat();
    
            System.out.println( "Please enter hours worked:" ); //input hours worked
            HoursWorked = input.nextFloat();
    
             GrossPay = HoursWorked * PayRate; // calculate gross pay
    
    
    
            if(PayRate > 0 && HoursWorked > 0)
    
            {
                System.out.printf(EmployeeName + "'s gross pay is $"  + "%6.2f",GrossPay);
    
            }
    
            else
    
                {
    
                System.out.println("Really? A negitive number? Let's assume that was an accident and try a positive number");
    
                }
    
    
            System.out.println(); // insert blank line
            System.out.println(); // insert blank line
            System.out.println(); // insert blank line
    
    At this point the loop has completed and should start again as long as the loop flag has not been met. What happens is the loop starts again, but I can't enter the employee name data. It prints the line then goes right to enter payrate which does work, but I am stuck in a loop.       
    
    It looks like this 
    
    Please enter employee name or stop to quit:
    tom
    Please enter employee pay rate:
    5
    Please enter hours worked:
    5
    tom's gross pay is $ 25.00
    
    
    Please enter employee name or stop to quit:
    Please enter employee pay rate:
    I can input data here
    
    
    }   // end of loop
    
    
    } // end of method
    
    } //end of class
    Thanks for any insight you can share.
    Last edited by bornwithnoname; 11-24-2008 at 02:58 AM.

    I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. (Terry Prattchet)

  2. Internet, Programming and Graphics   -   #2
    tesco's Avatar woowoo
    Join Date
    Aug 2003
    Location
    Canadia
    Posts
    24,069
    The problem is you're asking for the name after the loop starts.
    So if you enter 'stop' as the name it won't take effect until next time through the loop.
    Read: http://java.sun.com/docs/books/tutor...lts/while.html


    I've never done java before but I think you can use something like this:

    PHP Code:
    // Week two check point
    // Thomas Gamache IT-215

    import java.util.Scanner// program uses Scanner


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

            
    float PayRate 0;
            
    float HoursWorked 0;
            
    float GrossPay;
            
    String EmployeeName = new String ("");

            do
            { 
    // open loop

                
    System.out.println"Please enter employee name or stop to quit:" ); // input employee name
                
    EmployeeName input.nextLine();
                
                if (!
    EmployeeName.equalsIgnoreCase ("stop"))
                {
                    
    System.out.println"Please enter employee pay rate:" ); //input emplyee rate
                    
    PayRate input.nextFloat();

                    
    System.out.println"Please enter hours worked:" ); //input hours worked
                    
    HoursWorked input.nextFloat();

                    
    GrossPay HoursWorked PayRate// calculate gross pay

                    
    if(PayRate && HoursWorked 0)
                    {
                        
    System.out.printf(EmployeeName "'s gross pay is $"  "%6.2f",GrossPay);
                    }
                    else
                    {
                        
    System.out.println("Really? A negitive number? Let's assume that was an accident and try a positive number");
                    }


                    
    System.out.println(); // insert blank line
                    
    System.out.println(); // insert blank line
                    
    System.out.println(); // insert blank line
                
    }
            }   
    // end of loop
            
    while (!EmployeeName.equalsIgnoreCase ("stop"));
        } 
    // end of method
    //end of class 
    Even if it doesn't work at least it gets you in the right direction...

    I've also properly tabbed everything for you to make it easier to read.
    You then don't need the "// open loop" and "// end loop", etc. because you just follow the lines vertically.

  3. Internet, Programming and Graphics   -   #3
    Also you can do this:
    Code:
    while(true)
    {
         System.out.println("Please enter employee name or stop to quit:");
         EmployeeName = input.nextLine();
         if(EmployeeName.equalsIgnoreCase("stop"))
             break;
         
         System.out.println("Please enter employee pay rate:");
         // ...
    }
    Last edited by s_player; 11-09-2008 at 03:22 PM.

Bookmarks

Posting Permissions

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