Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: learning the try and catch

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post learning the try and catch

    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);
    Last edited by Norm; January 23rd, 2014 at 07:23 AM. Reason: quote tags changed to code tags


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: learning the try and catch

    This working nice try it out


    Code removed.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: learning the try and catch

    @jhansitenali: Please don't just post solutions or near-enough that could be used as solutions. Focus on the question and guide the OP to a solution, pushing lightly in the correct direction at first. Most are prohibited from using someone else's answer, and a solution with no explanation is often worthless for increasing the OP's understanding. For more info on why spoon feeding is discouraged here and on most forums that want to help people gain understanding, please read The Problem With Spoon-feeding.

  4. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Wink Re: learning the try and catch

    hi,

    I hope it will help you....

    try {
    System.out.println("\nWhat building information do you need?");
    b = scanIn.nextInt();  //Here itself you r getting exception so it wont go to the next line. The variable "b" having only initialized value "0". Thats why it is executing only "first building information
    b--;
    }

    You can do one thing put printing statments with in the "try" block. If you give correct number it wil print particular building information otherwise it wont print anything.

    Thanks,

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: learning the try and catch

    What do you want the program to do when there is an exception?
    If you want it to go back and ask the user to input a valid value, you need to use a loop that exits when a valid value is entered and loops back when the input is invalid.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: learning the try and catch

    Sorry for the late response, I haven't been taught much about loops yet, I think we used only one before but she was just showing an example of a code in class of what we will be able to do after taking this class. What kind of loop command could I use?

    Also with ganeprog comment, thanks

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: learning the try and catch

    You need to answer this question first:
    What do you want the program to do when there is an exception?
    then decide how to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: learning the try and catch

    I want the program to allow the user to input one of the 5 building numbers again after the exception. The programs purpose is to ask the user wat building information they want and when they input the number corresponding with the building it will display that building info, but if the user inputs and invalid number or letter then it will display an error message allowing the user to input a correct number. I just don't understand how to do that if someone could explain the steps, thanks

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: learning the try and catch

    allow the user to input one of the 5 building numbers again
    Doing something "again" is what loops are for. If your assignment requires the code to do things "again" then you need to use a loop.

    See the tutorial: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Try and catch
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2013, 05:58 AM
  2. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  3. [SOLVED] While try catch loop
    By Duaber in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 02:14 PM
  4. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  5. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM