Member 13620781 Ответов: 2

Почему следующий код не улавливает исключение


Почему следующий код не ловит исключение, когда пользователь вводит букву. Вместо этого исключение перехватывается, если пользователь вводит число, отличное от заданного выбора.

Это не полный код.

Что я уже пробовал:

while(true) {
			System.out.println("\nList of Attraction: ");
			System.out.println("\nSport: [1]Ferringhi Sea Sports Co.");
			System.out.println("Culture: [2]Pinang Peranakan Museum");
			System.out.println("Shops: [3]Queensbay Mall");
			System.out.println("Food: [4]Restoran Nasi Kandar Line Clear");	
			System.out.println("[5] Exit");			
			System.out.print("\nEnter no. of attraction for more details: ");
			int typeID = input.nextInt();
			
			//Sport s = new Sport(1, "ESCAPE Theme Park", 139.58, "828 Jalan Teluk Bahang, Teluk Bahang, Penang Island 11050, Malaysia", 4.5);
			try {
			if(typeID==1)
			{
				a[0] = new Sport(typeID, "Ferringhi Sea Sports Co.", "9AM-7:30PM", "Wave Runner Watersport, Foot Reflexology & Fish Spa", "Batu Ferringhi, George Town, Penang Island, Malaysia", 4.7);
				System.out.println(a[0]);
			}
			else if(typeID==2)
			{
				a[1] = new Culture(typeID, "Pinang Peranakan Museum", "-Adult (RM20.00)\n\t   -Child below 6 (Free)", "29, Church St, George Town, 10200 George Town, Pulau Pinang, Floor 2", "Monday to Sunday including Public Holidays from 9:30 am to 5 pm", 4.4);
				System.out.println(a[1]);
			}
			else if(typeID==3)
			{
				a[2] = new Shops(typeID, "Queensbay Mall", "The mall features 5 retail levels of international and home-grown brands including fashion, lifestyle, dining, entertainment, sports, electronics, home furnishing and department store.", "1F-78 Queensbay Mall, 100 Persiaran Bayan Indah Bayan Lepas, 11900 George Town, Penang", "10:30AM-10:30PM", 4.4);
				System.out.println(a[2]);
			}
			else if(typeID==4)
			{
				a[3] = new Food(typeID, "Restoran Nasi Kandar Line Clear", "6.50 for a plate of rice, fried chicken, egg, vegetables and a mix of gravies", "Beside 161 & 177 Penang Road, George Town, 10000 George Town, Pulau Pinang", "24 hours", 3.6);
				System.out.println(a[3]);
			}
			else if(typeID==5)
				break;
			else
				throw new InputMismatchException();
			}
			catch(InputMismatchException ex)
			{
				System.out.println("Input Error: Wrong Input!");
				input.nextLine();
			}
			} // end while loop for city1

2 Ответов

Рейтинг:
13

CPallini

Вы должны поймать исключение, вызванное Scanner.nextInt метод.


Рейтинг:
10

Richard MacCutchan

Ваш try находится не в том месте. Так и должно быть:

while(true) {
			System.out.println("\nList of Attraction: ");
			System.out.println("\nSport: [1]Ferringhi Sea Sports Co.");
			System.out.println("Culture: [2]Pinang Peranakan Museum");
			System.out.println("Shops: [3]Queensbay Mall");
			System.out.println("Food: [4]Restoran Nasi Kandar Line Clear");	
			System.out.println("[5] Exit");			
			System.out.print("\nEnter no. of attraction for more details: ");
    try {
			int typeID = input.nextInt();