-2

Hey guys im pretty new to coding but one of my projects is to check to see if a string can be parse into a double. It keeps printing an error when trying running the program.

Here is the code:

        public static void main(String[] args) {
            SimpleReader in = new SimpleReader1L();
            SimpleWriter out = new SimpleWriter1L();

          // Constant entered in by user as a string
            out.println("Welcome to constant approximator");
            out.println("Please enter in a constant to be estimated");
            String realConstant = in.nextLine();

        //Double variable created in order to reassign later
            double test = 0;

       //FormatChecker class and canParseDouble verifies if the string is truly a double. boolean method.
            FormatChecker.canParseDouble(realConstant);
       //Test reassign and converts
            test = Double.parseDouble(realConstant);
            out.print(test);
            in.close();
            out.close();
        }

    }

Here is the error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "pi"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at ABCDGuesser1Test.main(ABCDGuesser1Test.java:36)

3 Answers3

0

It happens because you type pi which is not recognized as π (pi) constant. What have you typed was a String and these characters are not convertible to a number.

If you want to enter any number including the special constant like pi, you have to check first if the input is a Number or String. In case it's String, you can try to match it with a defined constant like π or e and use their defined value in Java such as Math.PI.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

You should use the result of canParseDouble() not just call it. Something like this, I think:

if (FormatChecker.canParseDouble(realConstant)) {
   test = Double.parseDouble(realConstant);
   out.println(test);
} 
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
Mike B
  • 2,756
  • 2
  • 16
  • 28
0

As you say:

//FormatChecker class and canParseDouble verifies if the string is truly a double. boolean method.
        FormatChecker.canParseDouble(realConstant);

You know that this line calls a boolean method and will then return either true or false. However, you do not do any use of this returned value. If so, what's the point of even calling the function, right?

You are trying to check if the string realConstant is a double, the method checks it but you simply ignore it, here. I believe you have an error because whether it is truly a double or not, the rest of the code will run. In the case where the string is not actually a double, an error will appear since the rest of the code cannot compile.

You should then use an if statement such as:

if (FormatChecker.canParseDouble(realConstant)) {
    test = Double.parseDouble(realConstant);
    out.println(test);
} 

Also, I do not think you should expect an input of "pi" to return a double!

ChanceVI
  • 200
  • 1
  • 10