Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Locate and open the project from the last exercise - EmployeeMavenProj EmployeeProj.

  2. Create a new class EmployeeFileMain for this task. Give it a main() method.

  3. Define a static method in the same class - call it getInput(). This method will be responsible for interacting with the user. It should take one String parameter - a question to print out. It should read the answer from the terminal - declare and create an InputStreamReader using System.in as its constructor parameter.

    Code Block
    languagejava
    InputStreamReader isr = new InputStreamReader(System.in);
  4. Similarly, declare and create a BufferedReader using your InputStreamReader reference as its constructor argument. Use the readLine() method of BufferedReader to retrieve your keyboard input - that is the String the method should return. The readLine() method is declared to throw an IOException. We have two choices - either we wrap the call to the readLine() method within try block and declare a catch block, or we pass the buck. We will opt for the latter - declare your getInput() method as throwing IOException.

  5. Declare new static method: an inputEmployee() method which will ask for a new employee's details to be entered. From inputEmployee() call getInput() three times - first ask for the first name and store the returned value in a variable nfame. Secondly, ask for the last name - store the return value in lname. Then ask for the age and store the returned value in variable strAge. Convert this value to an int using static method Integer.parseInt() and store in the variable called age.

  6. Create an Employee object passing the three values into its constructor - you may need to create the constructor first!

  7. We need to handle the exception that may be thrown in the getInput() method. We will pass the buck again - declare your inputEmployee() method as throwing IOException. Now it will need to be handled in the main method - there is no other option.

  8. Within your main method, make a call to inputEmployee method - you will see that it won't compile. The call needs to be within try block and we need to provide catch block and capture the IOException. Print out a message that something went wrong.

    Code Block
    languagejava
    try{
      inputEmployee()
    }catch(IOException e){
      System.out.println("Problem occurred.")
    }
  9. Now run the application supplying the name and a numeric age.

  10. Run the application again but supply non-numeric age -i.e. abc. The application with crash since it can't cast abc into an int. You will see a stack trace from NumberFormatException.

  11. Let’s handle that exception. Declare the inputEmployee method as throwing NumberFormatException, in the main method include another catch block to handle it. Make it print

    Code Block
    languagejava
    e.getMessage() + " is an invalid age!"
  12. Run your application again and make sure it can handle non-numeric data.

...