Home > Back-end >  Issue with addressing variables from different method
Issue with addressing variables from different method

Time:10-24

I am just learning to code in Java and ran into a problem. The task description was following: "The class SalaryCountingWithMethods below asks for three floating point numbers (work hours, salary per hour and tax percent) and then counts the salary before and after taxes as well as the tax part of the salary based on the information given to the program." I have been stuck on the problem for days now and i just cannot find a solution. This is my progress so far:

import java.util.Scanner;
public class SalaryCountingWithMethods {
    public static void main (String [] args) {
        double hours, salaryPerHour, taxPercent, taxlessSalary, taxPart;
 
        hours =  askHours();
        salaryPerHour = askSalaryPerHour();
        taxPercent = askTaxPercent();
        
        taxlessSalary = countTaxlessSalary(hours, salaryPerHour);
 
        taxPart = taxlessSalary * taxPercent /100;
 
        System.out.println("\nSalary before taxes: "   taxlessSalary);
        System.out.println("Tax part of the salary: "   taxPart);
        System.out.println("Salary after taxes: "   (taxlessSalary-taxPart));        
    }
static Scanner reader = new Scanner(System.in);
    private static int askHours(){
        System.out.print("Type in the number of work hours: ");
        int hours = reader.nextInt();
        return hours;
        }
    
    private static int askSalaryPerHour(){
        System.out.print("Type in salary per hour: ");
        int perHour = reader.nextInt();
        return perHour;
        }
        
    public static int askTaxPercent(){
        
        System.out.print("Type in the number of work hours: ");
        int taxPercent = reader.nextInt();
        return taxPercent;
        }
private static double countTaxlessSalary(double hours, double salaryPerHour){
    double taxPercent = 0;
    double fullSalary = (float)hours * (float)salaryPerHour;
    double taxPart = (float)fullSalary * (float)taxPercent /100;
    double taxLess = (float)fullSalary - (float)taxPart;
    return taxLess;
    }
}

and i am getting the following error:

Your program returned a none-zero value. You should return 0 from the main() function.
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at SalaryCountingWithMethods.askHours(SalaryCountingWithMethods.java:29) at SalaryCountingWithMethods.main(SalaryCountingWithMethods.java:14)

CodePudding user response:

The class SalaryCountingWithMethods below asks for three floating point numbers
The issue might be you are dealing with floating-point numbers but the methods you are defining are accepting integer input. This will cause java.util.InputMismatchException error when you give double instead of integer.

private static int askHours(){
   System.out.print("Type in the number of work hours: ");
   int hours = reader.nextInt();
   return hours;
}

You should accept double as input not integer.

private static double askHours(){
   System.out.print("Type in the number of work hours: ");
   double hours = reader.nextDouble();
   return hours;
}

You should do the same for other two methods askSalaryPerHour() and askTaxPercent().

  • Related