0

I am new to Java, and am trying to make a program to see if a person has a normal temperature, not too low or too high.

I have been getting this error message:(when I'am input a double, not a int)

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.nextDouble(Unknown Source)
        at ekstra217.main(ekstra217.java:15)

Here is my code

import java.util.*;

class temp
{//klassen start
    public static void main(String[]args)
    {//main start


    Scanner tast=new Scanner(System.in);

    System.out.println("Write your temperatur!");
 //normal temperatur is between 36.5 and 37.5
    double temperatur=tast.nextDouble();
    if (temperatur<36.5)
    {
    System.out.println("Your temperatur is normal");
    } 
    else if(temperature>37.5)
    {//else if  starts
    System.out.println("You have over normal,you are sick");
    }//else if slutter

    else{
    System.out.println("You have normal temperature");
    }
}   
   } 
kristine
  • 11
  • 1

2 Answers2

3

You seems to be entering a non-double value as the input to your program and hence encountering InputMismatchException while executing:

tast.nextDouble();
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You may want to wrap your data reading into an if like this:

if (tast.hasNextDouble())
   tast.next.Double;
else {
   // print something and exit
   System.out.println("Incorrect temperature value!!!);
   System.exit(1);
   }
PM 77-1
  • 12,933
  • 21
  • 68
  • 111