Search code examples
javadouble

Error Java Scanner cannot be converted to double


its showing error at function call saying cant change scanner to double

import java.util.Scanner;

public class Main{
    
    public static double calculateGrossIncome(double hrsPerWeek, double hrlyPay){
        double weeklyPay = hrsPerWeek * hrlyPay;
        double result = weeklyPay * 52;
        return result;
        
        
    }
    public static void main(String[] args)
    {
        System.out.println("Enter HOurly Pay :");
        Scanner hrlyPay = new Scanner(System.in);
        hrlyPay.nextDouble();
        
        System.out.println("Enter Hours Per week :");
        Scanner hrsPerWeek = new Scanner(System.in);
        hrsPerWeek.nextDouble();
        
        double income = calculateGrossIncome( hrsPerWeek, hrlyPay);
        System.out.println("The employee toatl gross income is " + income);
     
        
    }
}

Solution

  • You only need one instance of scanner, and you should save the user input to a variable.

    public static void main(String[] args)
    {
        System.out.println("Enter HOurly Pay :");
        Scanner scanner = new Scanner(System.in);
        double hrlyPay = scanner.nextDouble();
        
        System.out.println("Enter Hours Per week :");
        double hrsPerWeek = scanner.nextDouble();
        
        double income = calculateGrossIncome( hrsPerWeek, hrlyPay);
        System.out.println("The employee toatl gross income is " + income);
       
    }