Hey everyone, I'm taking a java class and this is 3rd week. Right now we are learning about arrays and using the try/catch. What I need help with is with this code below, I am trying to just display information about buildings. The application is good but not with the try and catch statement. I'm trying to just display the message of "please enter a building number" when a user puts a letter instead of a number(InputMismatchException) and then the user would have to put in one of the numbers. But when it runs and i put in a letter, it reads the message, but it always outputs the first building information, any help? thanks
package username; import java.util.InputMismatchException; import java.util.Scanner; //TallBuildings public class TallBuildings { //compare heights of buildings public static void main(String[] args) { //create local variables int i = 0; int b = 0; Scanner scanIn = new Scanner(System.in); //create building array Building[] bldgs = new Building[5]; //create 5 buildings and save in array bldgs[0] = new Building(); bldgs[0].name = "Sky Tower"; bldgs[0].location = "China"; bldgs[0].year = 2013; bldgs[0].height = 2750; bldgs[1] = new Building(); bldgs[1].name = "Sears Tower"; bldgs[1].location = "Chicago"; bldgs[1].year = 21974; bldgs[1].height = 1451; bldgs[2] = new Building(); bldgs[2].name = "One World Trade Center"; bldgs[2].location = "New York"; bldgs[2].year = 1972; bldgs[2].height = 1368; bldgs[3] = new Building(); bldgs[3].name = "Empire State Building"; bldgs[3].location = "New York"; bldgs[3].year = 1931; bldgs[3].height = 1250; bldgs[4] = new Building(); bldgs[4].name = "Chrysler Building"; bldgs[4].location = "New York"; bldgs[4].year = 1930; bldgs[4].height = 1046; //display the 5 buildings while (i < 5) { System.out.println((i+1) + "-" + bldgs[i].name); i++; } //ask user which building they want try { System.out.println("\nWhat building information do you need?"); b = scanIn.nextInt(); b--; } catch(InputMismatchException ime) { System.out.println("Please enter in a building number"); } //Display building information System.out.println(bldgs[b].name); System.out.println(bldgs[b].location); System.out.println(bldgs[b].year); System.out.println(bldgs[b].height);