From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: A simple calculator

Solution: A simple calculator - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Solution: A simple calculator

- [Instructor] In the previous video, I gave you a simple challenge: create an application that accepts two values from the command line, parses the values as numbers, and adds them together. Here's my solution to the challenge. First, I use a scanner object. I wrap the scanner around System.in. In Java, that's an input string, and the scanner will now accept input from the command line. Next, I output a prompt. Notice I'm using print and not print line. And then I accept the input by calling the scanner object's next line method. I receive a string value, and then I use the double class's parse double method to parse that value. Now, I'm depending on the user typing a numeric value in, and if they get it wrong, it'll cause an exception and stop the program, but handling that sort of problem requires exception handling, which I'll talk about later in this series. Next, I do the same thing again. I accept another numeric value and save it as a double also, then I create a third double…

Contents