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 - Java Tutorial
From the course: Java 8+ Essential Training: Syntax and Structure
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Declare and initialize String objects3m 45s
-
(Locked)
Create and concatenate String values4m 34s
-
(Locked)
Convert primitive values to strings5m 30s
-
(Locked)
Build a String from multiple values10m 35s
-
(Locked)
Compare String values with methods5m 19s
-
(Locked)
Format numeric values as strings7m 2s
-
(Locked)
Parse String values4m 10s
-
(Locked)
Challenge: A simple calculator56s
-
(Locked)
Solution: A simple calculator1m 26s
-
-
-