I'm having trouble parsing a double from a string


#1

Good morning,

I am creating a calculator bot for a project for class and I am having a bit of trouble parsing a double from a string from the user. The source code can be found here. As you can see, the error comes when I try and convert the string “num1” into a Double.

Thanks in advance!


#2

Could this be the problem?

            double n1 = Double.parseDouble(num1);
                   ^
            System.out.println(num1 + " and...");
                                 ^

#3

I ran through the code with that change, but it didn’t seem to do anything. I would had an issue down the line with that. Thanks!


#4

I don’t know Java beyond the very surface. Can numbers be concatenated with strings?

In the CC Java course there is basic integer based calculator to add, subtract, multiply, divide and return a modulo. Since I was hung up on the lengthy instruction sequence just for printing I wrote my own methods to simplify things. Don’t know if I’m going out on a limb, though. Works great. Here is a snippet from the Calculator class…

  public static void print (String str) {
    System.out.println(str);
  }
  public static String ints (int num) {
    return String.format("%d", num);
  }
  // methods

  // above this line
  public static void main (String[] args) {
    Calculator calc = new Calculator();
    print(ints(calc.add(5, 7)));
  }

A subsequent method could be written to accomodate doubles.