-1

I am trying to output the salary for each shift, but when outputted the salary shows up as 0.00 how can i correct this problem ? I have tried declaring the salary as float but i was unsuccessful ............................................................................................................

package overtime.pay;
import javax.swing.* ;
import java.util.Scanner;
/**
 *
 * @author whitneykenny
 */
public class OvertimePay {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
      Scanner Hours_worked = new Scanner(System.in);
    System.out.println("Enter how many hours worked ");
    int total_hours = Hours_worked.nextInt();
    
    if (total_hours == 40||total_hours < 40){
    
            System.out.println("Employee did not make any over time ");
   
    
    } else {
            
            System.out.println("The employee made overtime");
    
    
    }
    
   final double first_shift = 17.00;
       final double second_shift = 18.50;
       final double third_shift =  22.00;
    
            Scanner selected_shift = new Scanner(System.in);
    System.out.println("Which shift was worked  ");
    String shift_worked = selected_shift.nextLine();
            
         double salary = 0 ; 
        
        
if (shift_worked == "first") {
 salary =  first_shift;  
 
 
    }  else if (shift_worked == "second")  
    
    {salary = second_shift;
            
            }else if (shift_worked == "third")
            {
            
            }


System.out.println("Your salary is " + salary );

   
    
    
    
    }
            
            
            
            }
    
      
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
Kenny05
  • 9
  • 1
  • 3
    The code in your question isn't formatted correctly (irregular and wrong indentation, no standard for where braces go, etc.), and this makes it very hard to read. Most code editors will have a "reformat code" option or similar to automatically fix that, and after doing so the code will be much easier to read. Maybe you'll even be able to see the issue, then. – john01dav Feb 17 '21 at 05:57
  • 1
    To compare String in java you don't have to use `==` like `shift_worked == "first"`. You should use the `equals` or `equalsIgnoreCase` method from String class: `if (shift_worked.equals("first"))` – Edgar Magallon Feb 17 '21 at 05:58
  • 1
    You don't have to create a new `Scanner` every time you want to get data from the user. Please refer to https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Abra Feb 17 '21 at 06:25

2 Answers2

2

Use shift_worked.equals("first") to compare strings. You cannot compare strings using ==.

Vijay
  • 89
  • 8
1

In Java String is an object and not a primitive type which implies '==' checks for reference equality to check if they're the same object. '.equals()' tests for value equality.

Here is a post about string comparison which might help you understand better How do I compare strings in Java?

Also look at the documentation for string literals.

Yogesh C K
  • 44
  • 3