Numeric Input

Taking numbers as input and then being able to use this input as a numeric value is not as easy as you might think. Remember when you take input from the keyboard it is saved like this:
    input += k.getkeyChar();
where input is of type String. You can't perform arithmetic on two Strings. You must convert the values to a numeric type such as int, float, or double. Here's a short example:


You should try entering decimal numbers and words in as input and see what happens.

The key lines that you should study are these:

          try{
            output = Integer.parseInt(input);
          }catch(NumberFormatException nfe){
            output = -555; //error code
          }
This is an example of exception handling. The Integer.parseInt method throws an exception if it is fed a String value which does not parse into an integer. So, we, as programmers, must handle the situation where a user may enter bad data and the try-catch construct helps us with this. You will notice that anytime a user enters bad input the value of output (an int variable) will be set to -555.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Assignment:
You will create a simple calculator that specializes in adding two values. You will have two input strings and one output int variable.