Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

JAVA Caulculator in Workspaces?

Hello TreeHouse forums,

Is it possible to create a Calculator ( JAVA ) in TreeHouse workspaces? I've tired simple one but without a success ( can't define anything except addition and even that's broke )

Input works fine, once i declare function + instead of addition it does this :

5 + 3 = 53 instead of 8

Also any other function like division are returning error

//import features
import java.io.Console;
//Class
public class Calculator {

    public static void main(String[] args) {
      Console console = System.console();
        // Type code below
        // Welcome message
      System.out.printf("You've just enabled the best calculator in the world!\n");
        //Input 1
     String firstInput = console.readLine("Enter first number:  \n");
      //Function Input
      String function = console.readLine("Please enter function:  \n");
      //Input 2
      String secondInput = console.readLine("Please enter second digit:  \n");
      //Results
      String result;
      //Functions List
      if (function.equals("+")) {
         result = firstInput + secondInput;
         System.out.printf("Result is :  %s\n", result);

      }

    }
}

Thank you in advance for suggestions ;)

2 Answers

Seth Kroger
Seth Kroger
56,403 Points

Because the inputs are all strings, you're concatenating 2 to strings together (String + String), not adding two numbers together (double + double, or int + int). You need to convert the inputs to numbers with either of the static methods Integer.parseInt(String) or Double.parseDouble(String).

String firstInput = console.readLine("Enter first number:  \n");
double firstNumber = Double.parseDouble(String);

Thanks for advice, it really works it's almost 100% correct ( 10+5 = 15,050 for example ) And what about other functions?

I've defined subtraction same as addition :

 if (function.equals("-")) {
     result = firstNumber - secondInput;
     System.out.printf("Result is :  %s\n", result);

  }

but it gives me the error: (also for any other function such as division )

Calculator.java:28: error: bad operand types for binary operator '-'
result = firstNumber - secondInput;
^
first type: double
second type: String
1 error

Seth Kroger
Seth Kroger
56,403 Points

You need to convert the second input into a number as well. Another thing you need to be aware of is when printing a number you need to use %d for a whole number or %f for a decimal number in place of %s.