EDIT: I'm pretty sure I just figured this out but I still have so many questions!!
I have 3 classes, the input reader class code was given to me, so I don't think I'm suppose to change that.
This is what my lab says to
InvalidInputException is a custom checked exception that you will write.
NumberReader has a single field, of type InputReader. It has a method that repeatedly prompts the user to type a non-zero whole number. The user will type a zero to stop the loop. After the loop has finished, the sum of the input numbers will be displayed on the screen. This method must have a try catch block to catch a thrown InvalidInputException and display an error message if the user types something that is not an integer.
I'm not asking you to do my homework, I want to know what you are doing and why.
I have a lot of questions about this too (these aren't textbook questions, they are MY questions):
1) Like why is the throw statement in the catch block? Shouldn't it be in the try block so when the program fails it catches itself and fixes the issue?
2) When you throw InvalidInputException how does it know what you typed in? There isn't a line that is sending 'number' to that Class.
3) Same with InputMismatchException, how does that know what you typed?
4) my lab says 'this method must have a try catch block', does that mean I need a try catch in NumberReader????
5) Arrgh!
/** * This class reads the number you have inputted * * @author (Lindsay LaPlante) * @version (Nov 24th / 2011) */ public class NumberReader { // instance variables - replace the example below with your own InputReader inputReader; /** * Constructor for objects of class NumberReader */ public NumberReader() { // initialise instance variables inputReader = new InputReader(); } /** * prompts user to type a non zero whole number */ public void promptUser() throws InvalidInputException { int sumOfAllNumbersTyped = 0; int numberTyped; do{ System.out.println("Input a non zero whole number: "); numberTyped = inputReader.getNumber(); sumOfAllNumbersTyped += numberTyped; }while(numberTyped != 0); System.out.println("The sum of all numbers typed: " + sumOfAllNumbersTyped); } }
import java.util.Scanner; /** * This class gets the number you have inputted * * @author (Lindsay LaPlante) * @version (Nov 24th / 2011) */ public class InputReader { private Scanner scanner; public InputReader() { scanner = new Scanner(System.in); } /** This method returns the number typed by the user. If the number is not an integer it throws a custom checked exception that will be caught by the calling method. @throws InputMismatchException, InvalidInputException */ public int getNumber() throws InvalidInputException { int number = 0; try { number = scanner.nextInt(); } // catch any non-integer input catch(java.util.InputMismatchException exc) { scanner.nextLine(); // clear the buffer throw new InvalidInputException("not a valid number!"); } return number; } }
/** * This class says what is a valid number and what isn't. * * @author (Lindsay LaPlante) * @version (Nov 24th / 2011) */ public class InvalidInputException extends Exception { // instance variables - replace the example below with your own private String message; /** * Constructor for objects of class InvalidInputException */ public InvalidInputException(String message) { // initialise instance variables this.message = message; } /** * Getter method for message * @return message */ public String getMessage() { return message; } /** * Overridden toString method * @return getMessage() */ public String toString() { return getMessage(); } }