0

I have problem with scanner. If I try to use double, console gives me error message:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextDouble(Scanner.java:2413) at test.test.main(test.java:9)

code

package test;

import java.util.Scanner;

public class test {
public static void main(String args[]){
    System.out.print("vlozte hmotnost: ");
    Scanner vstup = new Scanner (System.in);
    double hmotnost = vstup.nextDouble();

//------------------------------------------        

    System.out.print("vlozte drahu: ");
    Scanner vstup2 = new Scanner(System.in);
    double draha = vstup2.nextDouble();

//------------------------------------------

    double sila = hmotnost * 10;

//------------------------------------------

    double praca = sila * draha;
    System.out.print("praca je: ");
    System.out.print(praca);
    System.out.println(" Joulov");
Some guy
  • 1,210
  • 1
  • 17
  • 39
Kizej
  • 3
  • 5

2 Answers2

2

Type 1,5 instead of 1.5. Also, as mentioned above, You can use one scanner:

import java.util.Scanner;

public class test {
    public static void main(String args[]) {
        System.out.print("vlozte hmotnost: ");
        Scanner vstup = new Scanner(System.in);
        double hmotnost = vstup.nextDouble();

//------------------------------------------

        System.out.print("vlozte drahu: ");
        double draha = vstup.nextDouble();

//------------------------------------------

        double sila = hmotnost * 10;

//------------------------------------------

        double praca = sila * draha;
        System.out.print("praca je: ");
        System.out.print(praca);
        System.out.println(" Joulov");
    }
}
Asakura
  • 127
  • 3
  • 14
  • See the marked duplicate I mentioned, if you want to parse using your locale (so you can parse commas or periods specifically as decimal separators) you can use [`Scanner.useLocale`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useLocale(java.util.Locale)) – tnw Jul 24 '15 at 15:09
0

first of all you don't need two Scanners. A nextDouble() on first scanner would help you read the next value.

Also I don't see why you are getting errors. I tried you code and it works just fine.

Output :

vlozte hmotnost: 1.5
vlozte drahu: 4.5
praca je: 67.5 Joulov
digidude
  • 289
  • 1
  • 8