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 11 of 11

Thread: While try catch loop

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default While try catch loop

    I'm trying to program a try/catch exception while loop that continues till the user enters x to exit. So far i've gotten it to work but i'm having one issue with the console printing "Account x cannot be found" before the program exits.

    /**	Murach, J. and Steelman, A., ( 2007).  Murach’s Java SE6,
    	Training and Reference.  Fresno, CA: Mike Murach & Associates.
    	Modifications by Wm Bowers, 2007 – 2011
    	CustomerApp.java interacts with Customer, CustomerIO, Validator
         and NoSuchNumberException
         STARTER Class
    */
     
    import java.util.Scanner;
     
    public class CustomerApp	{
        public static void main(String args[])	    {
    		PriceHeading.getHeading("Assignment 3");//heading call
            // display a welcome message
            System.out.println("  Welcome to the Customer application");
            System.out.println();
     
            // create the Scanner object
            Scanner sc = new Scanner(System.in);
     
            // continue until user enters x
            String custNo = "y";
            while (!custNo.equalsIgnoreCase("x"))	{
                custNo = Validator.getString(sc, "  Enter a customer number (or an \"X\" to quit) ->  ");
                try	{
                    Customer cust = CustomerIO.getCustomer(custNo);
                    System.out.println("\n" + cust.getNameAndAddress());
                }
                catch (NoSuchCustomerException e){	
                	System.out.println(e.getMessage());	  }
            	} 
     
                System.out.println();
     
            }
        }
    public class Customer {
    	public String name, address, city, state, zipCode;
    	public String getNameAndAddress() {
    		return "  " + name + "\n\t\t   "
     
            + address + "\n\t\t   "
     
            + city + ", " + state + " " + zipCode; 
    	}
    }
    class NoSuchCustomerException extends Exception {
    	private String customerNumber;
    	public NoSuchCustomerException()
    	{
    	}
     
    public NoSuchCustomerException(String customerNumber){
    	super("  The customer number " + customerNumber.toUpperCase() + " does not exist");
    }
     
    public String getCustomerNumber(){
    	return customerNumber;
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: While try catch loop

    Have you run through this with a debugger to trace what's actually happening?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While try catch loop

    I'm no pro with the debugger but I don't get the exception message till the catch statement

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: While try catch loop

    Well, I'd recommend learning how to debug sooner rather than later. Most IDEs have a debugger that works pretty intuitively. But basically, you're asking us to do it for you, and we don't really have time to do that. Maybe if you condensed your problem into an SSCCE we could.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: While try catch loop

    i'm having one issue with the console printing "Account x cannot be found" before the program exits.
    Where in your program is that message being printed?

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While try catch loop

    At the end right before the press any key console exit message? I'm typing x to exit and its trying to find account x (which doesnt exist) first then it exits.
    Last edited by Duaber; June 23rd, 2011 at 01:17 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: While try catch loop

    Sorry, I meant, where in the code is the message printed. I do not see "Acccount" or "cannot be found" in the code you posted. Is the message printed in some other code?

    Try debugging your code by printing out the values of the variables as they change.

    Can you execute the program and copy the console here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While try catch loop

    Sorry Norm I mistyped the exception message.

    Jeremy Price
    Assignment 3
    June 23, 2011

    Welcome to the Customer application

    Enter a customer number (or an "X" to quit) -> p1001

    Barbara White
    3400 Richmond Parkway #3423
    Bristol, CT 06010
    Enter a customer number (or an "X" to quit) -> random
    The customer number RANDOM does not exist
    Enter a customer number (or an "X" to quit) -> x
    The customer number X does not exist

    Press any key to continue . . .
    Last edited by Duaber; June 23rd, 2011 at 01:40 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: While try catch loop

    What does your program do immediately after displaying the message:
    Enter a customer number (or an "X" to quit) ->
    and you enter something?
    What is the next statement executed after it reads your response?

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While try catch loop

    Got it figured out. Used a scanner before the while statement then used another scanner at the end. Continues and ends like it should

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: While try catch loop

    Another way is to use the break statement.
    After the user enters his response, use an if statement to see if he entered the quit signal and then break from the while loop.

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  2. How do I catch a AWT-EventQueue-0
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: November 5th, 2010, 09:41 PM
  3. try/catch..help needed soon as possible
    By safra in forum Exceptions
    Replies: 1
    Last Post: September 19th, 2010, 08:46 AM
  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