0

Hello I am learning Java so I am getting the following error an I do not understand why I am getting the error because I when I change the datatype to Int and then run the code, the code runs properly without any errors so I get a bit confused on what is really happening

Error Message

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.nextFloat(Scanner.java:2496)
at SecondProject/com.SecondProject.Fundamentals.workerPay(Fundamentals.java:22)
at SecondProject/com.SecondProject.Fundamentals.main(Fundamentals.java:8)

Code below

package com.SecondProject;

import java.util.*;

public class Fundamentals {

    public static void main(String[] args) {
        workerPay();
    

    }
    public static void workerPay() {
    
        float hours;
        float rates;
        float total_pay;
        float extra_hours;
        float tax;
    
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter in your hours of work: ");
        hours = sc.nextFloat();
    
        System.out.println(hours);
    
    
    }

}

1 Answers1

0

an integer is not a float but a float and an integer are both doubles

change:

hours=sc.nextFloat();

to:

hours=sc.nextDouble();
DCR
  • 14,737
  • 12
  • 52
  • 115
  • This means that I have to change my initialization of all the floats to double, and however I just tried that I changed now and tested it with a value ```12.6``` and I get an error same error –  Jul 27 '20 at 19:20
  • did you read in the rest of the line? after hours=sc.nextDouble(); add -> sc.nextLine(); – DCR Jul 27 '20 at 19:26