0

Thats the Code

  import java.util.Scanner;

/*Test*/
class Hello {
    public static void main(String[] args) {
        System.out.println("Hallo");// Test
        System.out.println("Taschenrechner");
double holaninos=420;
System.out.println("Meine Variable ist "+holaninos);
        Scanner input = new Scanner(System.in);
System.out.println("Gib die erste Zahl ein ");
double zahl1=input.nextInt();
System.out.println("Gib die zweite Zahl ein ");
double zahl2=input.nextInt();
double zahl3=zahl1*zahl2;
System.out.println("Ergebnis: " +zahl3);
        System.out.println("Bitte gib den Radius ein ");
        double radius = input.nextInt();
        double flaeche = (double) (radius * radius * Math.PI);
        System.out.println("flaeche " + flaeche);
        double umfang = (double) (radius * 2 * Math.PI);
        System.out.println("umfang " + umfang);
    }

}
Thats the error message
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Hello.main(Hello.java:12)

I have the same Problem when use Float instad of double but when im entering numbers without a comma it works. It also works when I use int instead of double.

  • Your scanner has multiple methods like "nextDouble" for example which allows you to enter numbers "with points" (commas are used in Germany but forget this annotation please, always use points) like 2.0. See also: https://stackoverflow.com/questions/14027537/why-am-i-getting-inputmismatchexception – Michael Kemmerzell Nov 24 '20 at 13:35
  • 2
    @MichaelKemmerzell "forget this annotation please, always use points" I can tell you from experience that this is not always acceptable. A better suggestion would be "convert it if needed", which is a whole other can of worms, to be fair, or use a parser that understands locales. – Federico klez Culloca Nov 24 '20 at 13:40

1 Answers1

0

You are missing the right understanding of data types. All your integer values are casted automatically to double:

double holaninos=420;

Will be converted to:

double holaninos= (double) 420; // which equals to 420.0

so rather use the following if you want to enter double values:

double zahl1=input.nextDouble(); // enter 5.0 for example

or:

int zahl1=input.nextInt(); // enter 7 for example

Some helpful links for you:

Java datatypes

Java Scanner

Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43
  • 1
    `java.util.Scanner` will use the System Locale when parsing double inputs unless you explicity tell it to use another Locale with `useLocale`. I assume OP will be working on a machine that uses a German system locale so he will either have to enter numbers with comma as a decimal separator or will have to tell its scanner to not use the german locale for parsing numbers. – OH GOD SPIDERS Nov 24 '20 at 13:47