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");
Answer
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");
}
}
No comments:
Post a Comment