// Arup Guha
// 4/6/2026
// Written in class, completed later to show different ways to
// handle an exception.

import java.util.*;

public class CatchingExceptions {

	final public static boolean FLAG1 = false;
	final public static boolean FLAG2 = true;
	final public static boolean FLAG3 = false;
	
	public static Scanner stdin;

	public static void main(String[] args) {
		
		// This is shared by all of our tests.
		stdin = new Scanner(System.in);
		
		// Run the test you want to.
		// If you run test 1 before test 2, then the loop in test2
		// will run once consuming the item entered in test 1.
		if (FLAG1) test1();
		if (FLAG2) test2();
		if (FLAG3) test3();
	}
	
	// Test the first method.
	public static void test1() {
		int number = readIntTest();
		System.out.println("Test 1 - You entered "+number);
	}
	
	public static void test2() {
	
		// Testing second method.
		int value = -1;
		while (value == -1) {
			
			// We try this, but if it throws the exception, the Scanner stdin has
			// a string stuck in its buffer...
			try {
				value = readIntTest2();
			}
			
			// Here we handle the exception.
			catch (InputMismatchException e) {
				
				// The error message.
				System.out.println("Sorry that is not a valid integer.");
				System.out.println("Please try entering an integer this time.");
				
				// This consumes what was left in the stdin stream.
				// This is important otherwise our loop will run infinitely!
				stdin.next();
			}
			
		}
		
		// Here is the answer we got.
		System.out.println("Test 2 - You entered "+value);
	}
	
	// Third test...
	public static void test3() {
	
		// Testing second method.
		int value = -1;
		while (value == -1) {
			
			// We try this.
			try {
				value = readIntTest3();
			}
			
			// Here we handle the exception.
			catch (InputMismatchException e) {
				
				// The error message.
				System.out.println("Sorry that is not a valid integer.");
				System.out.println("Please try entering an integer this time.");

				/*** Since fin is a local variable in readIntTest3, I don't need
				     to consume any string from a Scanner object.
				***/
			}
			
		}
		
		// Here is the answer we got.
		System.out.println("Test 3 - You entered "+value);
	}
	
	// Here we handle the exception.
	public static int readIntTest() {
		
		// Prompt.
		System.out.println("Please enter an integer.");
		int value = 0;
		
		// Try reading.
		try {
			value = stdin.nextInt();
		}
		
		// How I am handling it.
		catch (InputMismatchException e) {
			System.out.println("Sorry, that wasn't a valid integer. We'll assume you entered 0.");
		}
		
		// What I return.
		return value;
	}
	
	// Here we just throw the exception and don't handle it.
	public static int readIntTest2() throws InputMismatchException {
		System.out.println("Please enter an integer.");
		int value = stdin.nextInt();
		return value;
	}
	
	// Here we just throw the exception and don't handle it.
	public static int readIntTest3() throws InputMismatchException {
		Scanner otherScanner = new Scanner(System.in);
		System.out.println("Please enter an integer.");
		int value = otherScanner.nextInt();
		return value;
	}
}