-3
import javax.swing.JOptionPane;

import java.util.Scanner;

public class pay{

static Scanner sc;

public static void main (String[]args)

{
  double Regpay = 8;
  double OThour = Regpay * 1.5;
  double hours = 0;
  double gpay = Regpay * hours;
  double Totalpay = OThour + gpay;

  Scanner input = new Scanner(System.in);
  System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour");
  System.out.println("If workers work more than 40hrs, their pay will be time and a half");
  System.out.println("please enter your name");
  String name = sc.nextLine();

  System.out.println("How many hours did you work this month");
  hours = sc.nextDouble();

  System.out.println("How many hours overtime did you work");
  OThour = sc.nextDouble();




  if (hours<=40){
      Totalpay = gpay;
      System.out.print("You are not entitled to overtime pay");
  }else{
      if (hours>=41)
      Totalpay = gpay + OThour >=1;   
    }
  }        
}

I am getting the error on the line Totalpay = gpay + OThour >=1; I don't know what to do, I keep trying to change things around but I keep getting the same error!!!!

Brick
  • 3,998
  • 8
  • 27
  • 47
  • What are you trying to accomplish with that line? It looks like you are comparing `gpay + OThour` to see if it's greater than or equal to `1`. – rgettman Nov 05 '15 at 19:39
  • 1
    `gpay + OThour >= 1` checks to see if `gpay + OThour` is greater than or equal to 1 and returns either `true` or `false`, which cannot be added to a `double` because it is not a number. – Bethany Louise Nov 05 '15 at 19:41

3 Answers3

0

The answer you are looking for ( I presume) is to test if OTHoure is greater than 1, add it to TotalPay. To accomplish this, replace TotalPay = TotalPay + OTHours >=1 with

if(OTHours>=1){
    TotalPay = gPay + OTHour;
}
Jaboyc
  • 567
  • 1
  • 8
  • 16
  • Most of this was done with my teacher but the aim is to add the over time hours (OThours) to the gpay to get the Total amount paid that month to the employee. The aim also was that if the employee has worked 41 hours or more then he shall be paid 8 x 1.5 for every hour he has worked over the 40 hours – Ricardo Romane Nov 05 '15 at 20:03
0

You're combining two incompatible expression types.

Totalpay = gpay + OThour >=1;

Using order of operations, "gpay + OThour >=1" is two expressions:

gpay + OThour : (the sum of two doubles)

(result of gpay + OThour) >= 1  : (a boolean expression)

The result of this is a boolean answer (true/false), which is not resolvable as a double, so it can't be stored in TotalPay.

Secondly, your results will not be as expected - the variable you're using (gpay) has been initialized to 0 and never gets changed.

purpleorb
  • 21
  • 6
0

@purpleorb and @ Jaboyc have already explained why the error is being thrown. I'm going to talk about a more holistic solution.

There are a couple of problems with this

  1. Finding values for variables before getting input from user
  2. Boolean being assigned to double problems (as you've mentioned)
  3. Unnecessary if-statement structure

You could change the code to as following to fix these issues:

  Scanner input = new Scanner(System.in);
  System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour");
  System.out.println("If workers work more than 40hrs, their pay will be time and a half");
  System.out.println("please enter your name");
  String name = input.nextLine();

  double hours = 0;
  System.out.println("How many hours did you work this month");
  hours = input.nextDouble();

  double Regpay = 8;
  double OThour = Regpay * 1.5;
  double gpay = Regpay * hours;
  double Totalpay = OThour + gpay;

  if (hours>40){
      Totalpay = gpay + (OThour- 1) * (hours - 40);   
  }else{
      Totalpay = gpay;
      System.out.print("You are not entitled to overtime pay");
    }

What this does is do the calculations gpay = Regpay * hours and Totalpay = OThour + gpay after the user has inputted a value into hours. Also, this simplifies your if statement structure by checking if the number of hours is greater than 40, then performing the desired calculations afterwards.

Bimde
  • 722
  • 8
  • 20
  • Your welcome! If you find an answer helpful, and it answers your question, remember to click the green check-mark to mark it as the accepted answer! – Bimde Nov 06 '15 at 03:03