java.util.InputMismatchException

Help me figure it out, I'm a newbie. Mistake:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at kalkulator.main(kalkulator.java:36) "

Here is the code:

import java.util.Scanner;
public class kalkulator {
    public static void main (String[]args){
        Scanner vod = new Scanner(System.in);
        int cheslo1,cheslo2,rezultat;
        String diya;

        System.out.println("Введіть перше число:");
        cheslo1 = (vod.nextInt());

        System.out.println("Введіть друге число:");
        cheslo2 =(vod.nextInt());

        System.out.println("Виберіть дію +-/*");
        diya =vod.nextLine();
        if(diya.equals("+"))
        {
            rezultat =cheslo1+cheslo2;
        }
        else if(diya.equals("-"))
        {
            rezultat=cheslo1-cheslo2;   
        }
        else if(diya.equals("/"))
        {
            rezultat=cheslo1*cheslo2;
        }
        else if(diya.equals("*"))
        {
            rezultat=cheslo1+cheslo2;
        }

        cheslo2 = (vod.nextInt());
        rezultat=cheslo1 + cheslo2;
        System.out.print("Результат:");
        System.out.print(rezultat);
    }
}

2 answers

import java.util.Scanner;

public class kalkulator {
    public static void main(String[] args) {
        Scanner vod = new Scanner(System.in);
        double cheslo1, cheslo2, rezultat = 0;
        String diya;

        System.out.println("Введіть перше число:");
        cheslo1 = (vod.nextDouble());

        System.out.println("Введіть друге число:");
        cheslo2 = (vod.nextDouble());

        System.out.println("Виберіть дію +-/*");
        // diya =vod.nextLine(); 
        diya = vod.next(); 

        if (diya.equals("+")) {
            rezultat = cheslo1 + cheslo2;
        } else if (diya.equals("-")) {
            rezultat = cheslo1 - cheslo2;
        } else if (diya.equals("/")) {
            rezultat = cheslo1 / cheslo2;
        } else if (diya.equals("*")) {
            rezultat = cheslo1 * cheslo2;
        }
        //cheslo2 = (vod.nextInt());
        // rezultat=cheslo1 + cheslo2;
        System.out.print("Результат:" + rezultat);
    }
}

To read a string from the input stream, use the next() or nextLine() methods.

Scanner methods used:

next (); - reads the entered string to the first space

nextLine (); - reads the entire entered string

nextInt (); - reads the entered number int

nextDouble (); - reads the entered number double

 0
Author: invzbl3, 2018-07-21 12:16:01

In general, the modified version below with the changed variable names does not generate an exception:

public static void main(String[] args){
    double val, val1, result;
    String symbol;

    try(Scanner scan= new Scanner(System.in)){
        System.out.println("Введите первое число: ");
        val = scan.nextDouble();

        System.out.println("Введите второе число: ");
        val1 = scan.nextDouble();

        System.out.println("Введите знак: ");
        symbol= scan.next();
    }


    switch (symbol){
        case "/" : result = val / val1;
        break;

        case "*" : result = val * val1;
        break;

        case "+" : result = val + val1;
        break;

        case "-" : result = val - val1;
        break;

        default: result = -1;
        break;
    }

    System.out.println(result);
}

The error could have occurred because:

  • You don't close the vod thread .
  • You are using nextLine() incorrectly.
  • Why do you assign a value to the variable cheslo2 for the second time?
 0