3

How am I able to get rid of one scanner? If I do use just one scanner the after the weekly_pay is output is:

Employee Name
Enter the hours worked for the week.

The program skips right over asking for the employee name variable. With both scanners it does indeed loop asking for the employee name as it should.

//Week 3 Assignment 
package weeklypay2;

import java.util.Scanner; // importing the Java utility class scanner 

public class WeeklyPay2 // class WeeklyPay
{
    //main method
    public static void main(String[] args)
    {
        Scanner Scanner = new Scanner(System.in);
        Scanner Scanner1 = new Scanner(System.in);

        String employee_name = null; // variable for employee name
        double hours_worked = 0, // variable for weekly hours worked
        pay_rate = 0, // variable for pay rate per hour
        weekly_pay = 0; // weekly pay = hours * pay_rate

        while (employee_name!="stop")
        {
            System.out.println("");
            System.out.println("Employee Name"); // prompt, employees name
            employee_name = Scanner1.nextLine();

            if (employee_name.equalsIgnoreCase("stop"))  
            {  
                System.out.print("Exiting Program");  
                break;  
            } // ends if statement  
            else
            {  
                System.out.println("Enter the hours worked for the week");
                //prompt, hours worked for the current week
                pay_rate = Scanner.nextDouble();

                while(pay_rate < 0.01)
                {
                    System.out.print("ERROR!!, Input a postive number: \n");
                    pay_rate = Scanner.nextDouble();
                }

                System.out.println("Enter the employees hourly pay rate"); 
                // prompt, the employees pay rate
                hours_worked = Scanner.nextDouble(); 

                while(hours_worked < 0.01)
                {
                    System.out.print("ERROR!!, Input a postive number: \n");
                    hours_worked = Scanner.nextDouble();
                }

                weekly_pay = (double) hours_worked * (double) pay_rate; // setting the variable weekly_pay
                System.out.println(employee_name + "'s weekly pay is $" + weekly_pay ); // output the employees name and weekly pay
            }
        }
        Scanner.close();
        Scanner1.close();
    } //ends main method
} //ends class WeeklyPay
Tom
  • 16,842
  • 17
  • 45
  • 54
James
  • 69
  • 7
  • 2
    `while (employee_name!="stop")` please read this: [How to compare strings in java?](http://stackoverflow.com/q/26854645) – Tom May 29 '15 at 16:59
  • @Tom I have misunderstood the question. My bad. – Vince May 29 '15 at 17:04
  • @VinceEmigh Ok :). His problem is answered in the first question that I linked (just if like to know the answer as well). – Tom May 29 '15 at 17:05

2 Answers2

1

use Scanner1.nextLine() after getting the value for hours worked.

Refer modified code below:

import java.util.Scanner; // importing the Java utility class scanner

public class WeeklyPay2 // class WeeklyPay
{
    //main method
    public static void main(String[] args) {

        Scanner Scanner1 = new Scanner(System.in);

        String employee_name = null; // variable for employee name
        double hours_worked = 0, // variable for weekly hours worked
                pay_rate = 0, // variable for pay rate per hour
                weekly_pay = 0; // weekly pay = hours * pay_rate

        //Scanner1.useDelimiter("//n");
        while (true) {

            System.out.println("");
            System.out.println("Employee Name"); // prompt, employees name

            employee_name = Scanner1.nextLine();

            if (employee_name.equalsIgnoreCase("stop"))

            {

                System.out.print("Exiting Program");

                break;

            }// ends if statement

            else {

                System.out.println("Enter the hours worked for the week");
//prompt, hours worked for the current week
                pay_rate = Scanner1.nextDouble();

                while (pay_rate < 0.01) {
                    System.out.print("ERROR!!, Input a postive number: \n");
                    pay_rate = Scanner1.nextDouble();
                }

                System.out.println("Enter the employees hourly pay rate");
                // prompt, the employees pay rate
                hours_worked = Scanner1.nextDouble();

                while (hours_worked < 0.01) {
                    System.out.print("ERROR!!, Input a postive number: \n");
                    hours_worked = Scanner1.nextDouble();
                    //Scanner1.
                }

                Scanner1.nextLine();
                weekly_pay = (double) hours_worked * (double)
                        pay_rate; // setting the variable weekly_pay

                System.out.println(employee_name + "'s weekly pay is $" + weekly_pay); // output the employees name and weekly pay

            }
        }

        Scanner1.close();

    } //ends main method


}//ends class WeeklyPay
Prabhakaran
  • 169
  • 2
  • 12
1

Well the first thing I would say is be careful naming your scanner. Don't start that with a capital. Now a big thing is you don't want to compare strings using !=, this is not intended for string variables. There is a fun method for comparing strings that would be .equals() or .equalsIgnoreCase() depending if you want it non-case sensitive. Now after changing those few things in your code it works just fine. Also just a heads up Java also includes a method for formatting currency. You're pay numbers were missing a decimal. If you look at the NumberFormat I added after the scanner and then look at the println at the end you can see its very simple to use.

  import java.text.NumberFormat;
  import java.util.Scanner; // importing the Java utility class scanner 

  public class WeeklyPay2 // class WeeklyPay
  {
 //main method
 public static void main(String[] args)
{
    Scanner scanner = new Scanner(System.in);
    NumberFormat format = NumberFormat.getCurrencyInstance();
    String employee_name = "";
    double hours_worked = 0, // variable for weekly hours worked
    pay_rate = 0, // variable for pay rate per hour
    weekly_pay = 0; // weekly pay = hours * pay_rate

    while (!employee_name.equalsIgnoreCase("stop"))
    {

        System.out.println();
        System.out.print("Employee Name: "); 

        employee_name = scanner.nextLine();

        if (employee_name.equalsIgnoreCase("stop"))  
        {  
            System.out.print("Exiting Program");  
            break;
        } // ends if statement  
        else
        {  
            System.out.print("Enter the hours worked for the week: ");
            //prompt, hours worked for the current week
            pay_rate = scanner.nextDouble();

            while(pay_rate < 0.01)
            {
                System.out.print("ERROR!!, Input a postive number:  ");
                pay_rate = scanner.nextDouble();
            }

            System.out.print("Enter the employees hourly pay rate: "); 
            // prompt, the employees pay rate
            hours_worked = scanner.nextDouble(); 

            while(hours_worked < 0.01)
            {
                System.out.print("ERROR!!, Input a postive number: \n");
                hours_worked = scanner.nextDouble();
            }

            weekly_pay = (double) hours_worked * (double) pay_rate; // setting the variable weekly_pay
            System.out.println(employee_name + "'s weekly pay is: " + format.format(weekly_pay)); // output the employees name and weekly pay
            scanner.nextLine();
        }
    }
    scanner.close();

} //ends main method
} //ends class WeeklyPay

And this is the output we would see:

enter image description here

Oh, PS: Keep the program cleaner by prompting for the input on the same line, more intuitive and better to look at. By all means add a line break after the input but not before :)

basic
  • 3,348
  • 3
  • 21
  • 36
  • 1
    Now you can't enter the full name of an employee anymore. – Tom May 29 '15 at 18:30
  • 1
    Ah @Tom you were correct sir. All fixed now! See edit OP. Change to nextLine() like said but at the end of the loop add a scanner.next() to discard all junk :) – basic May 29 '15 at 18:53