0
// Week 3 Checkpoint1: Payroll Program Part 2
// Due May 04, 2012
// Created by: Kennith Adkins

import java.util.Scanner;

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

        // Variables
        String employeeName = null;
        int hours;
        double rate;
        double pay;

        while ( employeeName != "stop")
        {
        // Request information from user
        System.out.print ( "Employee Name: ");
        employeeName = input.nextLine();
        System.out.print ( "Hourly Rate: ");
        rate = input.nextDouble();
        System.out.print ( "Number of Hours worked this week: ");
        hours = input.nextInt();

        // Calculate pay
        pay = rate * hours;

        // Display information
        System.out.printf ("%s will get paid $%.2f this week.\n", employeeName, pay);


        }
    }
}

When I run the program it runs fine. When it hits the loop and repeats, Employee Name: and Hourly Rate seem to bunch up. Also how would I get it to immediately stop after typing stop as employee Name?

Bart
  • 19,692
  • 7
  • 68
  • 77

1 Answers1

0

As this appears to be a homework question I'll point you in a learning direction.

So for the employee name question I will redirect you to How do I compare strings in Java?

The scrunching issue is because when you reenter the loop and call scanner(input).nextLine it ends up actually reading the input text that it had not seen yet. So one option is to switch to something else lick a BufferedReader or move the scanner deceleration down in to the loop.

More research

I haven't worked much with the scanner class and after seeing this I was somewhat confused. The issue is actually the nextInt and nextDouble. They dont claim the return charter and as such when you call next line next time it is picking up the leftover return character.

So my option of reseting the scanner when reentering works in this specific case but you should either use 2 scanners or move off of scanners.

Scanner txtinput = new Scanner(System.in);
Scanner numberinput = new Scanner(System.in);
Community
  • 1
  • 1
Krrose27
  • 1,026
  • 8
  • 13
  • I changed the prints to printlns and it still comes up with the same issue. I appreciate the link. –  May 05 '12 at 03:40
  • Can you show me exactly what you mean by scrunching? I may not be understanding that. – Krrose27 May 05 '12 at 03:41
  • When I run the program, it works perfect and then when it loops, it scrunches like so employee Name: hourly Rate: (this is where it lets me start typing again –  May 05 '12 at 03:45
  • @Usdblades I realized the issue is the scanner. Read above. – Krrose27 May 05 '12 at 04:09
  • that seemed to take care of that problem. Sorry took so long to respond. Reading that link you gave me :). Thank you again –  May 05 '12 at 04:44