Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
It's time to put some of what we've talked about to work in a real application. This is going to be a very tiny almost trivial application that does little bit of calculation. It will accept input from the command line and then evaluate values that the user types in as numbers and add them together, not too complicated, but it will require setting variables, converting values, and doing a few other things that I have taught how to do in this chapter. I'm working in a project named SimpleCalc that's available in the Exercise Files. The calculative class in this project has a main method that in turn makes a call to a method called getInput, the getInput method is down here, the purpose of getInput method is to allow the user to type a value in and then return it as a string.
I'm not going to talk right now about the different classes that are used, the BufferedReader and the InputStreamReader, and in fact, for this lesson I'm recommending using this method as a bit of black box. Just call the method knowing you'll get back a String value and to make things less complicated I'm going to collapse the code by clicking on the little minus icon right next to the method name. And that leaves me with a very small view of my code showing the details of the main method. and the fact that there is a getInput method that returns a String. The application in its current state calls the getInput method once and returns the value as a variable called s1 and then outputs that value to the console.
I'll run the application. The value I pass into the method enter a value is displayed as a prompt, I'll type in a value, some value and press Enter and that value is echoed to the console. So I know that the input method is working correctly. Now the goal of this application is to receive two numeric values, so I'm going to change the prompt on this first call to getInput and I'll change the prompt to Enter a numeric value, then I'm going to copy and paste that line of code and I'll change the variable that's being returned to s2.
I don't need this output for the moment so I'm just going to commented out by pressing Ctrl+/ on Windows or Command+/ on Mac. Now the next step is to convert these Strings to numeric values, I want my two Strings to turn into two double values. So I'm going to declare a double value called d1 and I'm going to convert the String to a double value using this method of the double wrapper class, Double.parseDouble and I'll parse in s1. Then I'll do the same thing for the second value double d2= Double.parsedouble(s2).
And so now I have two numeric values. Now I'm going to add the two values together, I'll create a third double value called result and now add d1+d2; Now because I cast the numeric values as doubles originally, they'll correctly add together and create a third double. And then finally, I'll uncomment this command, System.out.println and I'll change the output to "The answer is" and I'll append result. So now I'm asking the user to enter a two values both numbers as long as they can be parsed as double values, the mathematical operation should work.
I'll run the application; I'll click into the console and type in an initial value of 10 and a second value of 25.5. I'm using a fractional value because I know I cast these values as doubles and they can accept that. I'll press Enter and there is the result, The answer is 35.5.Now, let's take a look at what happens if the user types in a non-numeric value, I'll click the Run button again and this time I'll type in a value of xyz. So far so good nothing bad is happened and that's because I haven't tried to parse the String as a number yet, I've just gone to the next line that asks for another String, so I'll type abc, now when I press Enter the parsing will begin and I'll get an error, because xyz can't be translated as a numeric value, specifically as a double value, I get an exception called java.lang.NumberFormatException.
Now, I haven't shown you the tools you need to handle this exception yet, so for now this simple calculative will only work if the user types in correct values, that is String values that can be parsed and turned into numbers without creating exceptions, but if the users does what they are supposed to do directly, then the application will work. In later chapters I'll show you more about Strings and more specifically about Error Handling, so that you can easily capture this kind of problem and tell the user what kind of input you need.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99249 Viewers
56 Video lessons · 112522 Viewers
71 Video lessons · 81330 Viewers
131 Video lessons · 39045 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.