4 Replies - 943 Views - Last Post: 23 June 2011 - 12:07 PM Rate Topic: -----

#1 Duaber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 11

While try catch loop

Posted 23 June 2011 - 11:10 AM

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.
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) ->  ");
        //gets customers name and address
            try	{
                Customer cust = CustomerIO.getCustomer(custNo);
                System.out.println("\n" + cust.getNameAndAddress());
            }
            catch (NoSuchCustomerException e){	
            	System.out.println(e.getMessage());	  }
        	} 
 
            System.out.println();
 
        }
    }

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;
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: While try catch loop

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: While try catch loop

Posted 23 June 2011 - 11:25 AM

Quote

but i'm having one issue with the console printing "Account x cannot be found" before the program exits.

Because when you enter x, it first searches for that customer x, that is why it go the catch block since the customer is not found
Was This Post Helpful? 1
  • +
  • -

#3 Duaber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 11

Re: While try catch loop

Posted 23 June 2011 - 11:38 AM

You are correct now I gotta find out a way to immediately exit to program when x is typed
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: While try catch loop

Posted 23 June 2011 - 11:59 AM

Yes, I may suggest one way, instead of having one input statement inside while, you may have two like this:
 String custNo = Validator.getString(sc, "  Enter a customer number (or an \"X\" to quit) ->  ");
while (!custNo.equalsIgnoreCase("x"))   {
//put all your code for while except that statement which we put it outside
//.....
//Then at the end of while ask again for input
 custNo = Validator.getString(sc, "  Enter a customer number (or an \"X\" to quit) ->  "); 

This will make sure you only enter loop if the input is not x and you will not need to make custNo = "y" at start
Was This Post Helpful? 1
  • +
  • -

#5 Duaber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-June 11

Re: While try catch loop

Posted 23 June 2011 - 12:07 PM

Thanks so much for the suggestion it worked like a charm! I repped you for your time and effort.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1