-3

I'm trying to create a program where the user can calculate their daily salary based on when they arrived and left work. I have however received the same error message for the past hour, any help would be greatly appreciated.

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at Tidsrapport.main(Time.java:17)

My code:

import java.util.Scanner;

public class Time {

        public static void  main(String[] args)
        {
        int hours, minutes, totalhours;

        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\\s*:\\s*");

        System.out.print("When did you arrive at work today [hh:mm]>");
        keyboard.nextInt();

        System.out.print("When did you leave work today [hh:mm]>");
        keyboard.nextInt();

        hours = keyboard.nextInt();
        minutes = keyboard.nextInt();

        totalhours= hours + (minutes/60);
        double salary = totalhours * 9;

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

        }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Spyro
  • 11
  • 5

3 Answers3

2

Many a problems:

System.out.print("When did you arrive at work today [hh:mm]>");
keyboard.nextInt();

An input like 10:15 ... is not an int. You have to use

next();

instead - to return a String; and then you have to validate that the incoming string consists of number number colon number number for example.

But of course, none of that matters, as you not dong anything with the value provided by the user. You need something like:

String rawArrivalTime = keyboard.next();

And then you can work with that local variable rawArrivalTime; for validating, parsing, ...

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

From the JavaDocs, the nextInt() method will throw these exceptions under these conditions:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

Use hasNextInt() first to validate if it is really an int.

SOFe
  • 7,867
  • 4
  • 33
  • 61
0

Well, the other two people kinda answered you, so here are some tips on how to work with error messages:

  1. Exception in thread "main", as long you write programs that use one thread, it is always gonna say in thread "main". Simply cause no other threads exsist.
  2. java.util.InputMismatchException this is one is important, it tells you why the program could not continue.
  3. The lines starting with at. This is the call stack. Basically a list of functions that are currently being invoked (currently = when it crashed).
    So if you see that you're current function is actually correct, but gets bad arguments. Then you use this to see where did the "bad arguments" come from. So this one is telling where the program could not continue.

Hope it helps you with future bugs.

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69