3

I'm a complete beginner and trying to write a code to print the factorial of a number. I want the user to be able to enter any possible number they want, but if they enter a number that is not a positive integer, the program tells them they have to try again. But when I run the code and try entering '5.5', I get a compilation error. I cannot figure out why. What is going wrong?

package javaExercises;

import java.util.Scanner;
public class Fac {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        try {
            double result = 1; //initialize our result variable 

            System.out.println("Enter a positive  integer: ");
            double fac = in.nextDouble();

            if (fac != (int) fac)
                System.out.println("Your entry is not an integer.");
            else if (fac < 0)
                System.out.println("Your entry is negative.");
            else if (fac == 0)
                System.out.println("The factorial of 0 is 1.");
            else {
                for (double i = fac; i > 1; i--)
                    result *= i;

                System.out.println("The factorial of " + Math.round(fac) + " is " + Math.round(result) + ".");
            }

        } finally {
            in .close();
        }
    }
}

The error I am getting:

Enter a positive integer: 4.5 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.nextDouble(Scanner.java:2564) at 
javaExercises.Fac.main(Fac.java:16) –
FlexEast
  • 317
  • 2
  • 13
ai.jennetta
  • 1,076
  • 2
  • 10
  • 25
  • 2
    You get a runtime error, the program is already compiled. – Federico klez Culloca Jun 26 '18 at 13:43
  • what is the error you are receiving? – Nitishkumar Singh Jun 26 '18 at 13:43
  • `run the code and try entering '5.5', I get a compilation error` error encountered in running state is not a compilation error – soufrk Jun 26 '18 at 13:43
  • *"But when I run the code and try entering '5.5', I get a compilation error."* If you're running the code, it's not a compilation error. Compilation errors occur during compilation, which is *before* you run the code. If it's happening when running the code, it's a *runtime* error. Either way, when asking about an error, it's important to copy and paste the error to the question. – T.J. Crowder Jun 26 '18 at 13:43
  • the error is Enter a positive integer: 4.5 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.nextDouble(Scanner.java:2564) at javaExercises.Fac.main(Fac.java:16) – ai.jennetta Jun 26 '18 at 13:44
  • 3
    Please add the exception as part of question and not as comment – soufrk Jun 26 '18 at 13:44
  • 3
    Are you in a location which uses comma `,` as a decimal point? – Peter Lawrey Jun 26 '18 at 13:44
  • Are you sure you're not writing `5,5` with a comma? – Federico klez Culloca Jun 26 '18 at 13:45
  • @PeterLawrey yes this could possibly be the reason of exception +1 – bit-shashank Jun 26 '18 at 13:45
  • Here's what you need, [nextDouble](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()) – soufrk Jun 26 '18 at 13:46
  • @soufrk - Notice the use of Scanner. – T.J. Crowder Jun 26 '18 at 13:46
  • 3
    Just a side note, "Enter a positive integer" is an odd way to prompt for a possibly-fractional number. – T.J. Crowder Jun 26 '18 at 13:47
  • Yes, it was the comma! I didn't know I needed a comma instead of a decimal ,thank you all! – ai.jennetta Jun 26 '18 at 13:47
  • okay, well now it works when I type '5,5' but it doesn't work when I type '5.5' so I am very confused now. – ai.jennetta Jun 26 '18 at 13:49
  • @jgcello - Ignore my (deleted) comment about `nextDouble`, it says it uses the locale-sensitive decimal separator, which is apparently `,` in your locale: https://docs.oracle.com/javase/9/docs/api/java/util/Scanner.html#nextDouble-- – T.J. Crowder Jun 26 '18 at 13:50
  • Don't forget to add the exception trace to the actual question, as @soufrk mentioned. – T.J. Crowder Jun 26 '18 at 13:56

1 Answers1

5

It´s the behaviour of the default-location of your PC or more detailed the used numeric formatters for your location set on the JVM. Date does the same btw.

So for example I´m german and got this setting on my PC. Now I need to enter digits like this: 5,5 because , is the default separator in germany. Same happens witch date/... so 23. Nov will be 23/11 and not 11/23 like in US.

you can change default input types using this:

Scanner scanner = new Scanner(System.in).useLocale(Locale.US); // 5.5, DE=5,5, ....

It´s not a compilation error because the programm can be compiled and started. The error is triggerd while running so it´s a runtime-error.

LenglBoy
  • 1,451
  • 1
  • 10
  • 24