Welcome to Dream.In.Code
Become a Java Expert!

Join 149,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,313 people online right now. Registration is fast and FREE... Join Now!




exception handling

 
Reply to this topicStart new topic

exception handling

dogz
17 May, 2007 - 07:54 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2007
Posts: 13


My Contributions
this code is the driver code for a program i have been working on, the only problem i have left with it is that with the exception handling. when a string is enteres in either the quantity or price the warning come up but then it goes straight to the nex dialog box instead of repeating the previous question. what can i do?

CODE
public class TestInvoice
{
    
    public static void main(String[] args)
    {
        String input;
        double number;
        double invoiceTotal = 0;
        
        Invoice myInvoice[] = new Invoice[3];
                
        for (int index = 0; index < 3; index++)
        {
            double num = 0;
            
            myInvoice[index] = new Invoice();
            
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Number");
            myInvoice[index].setPartNumber(input);
        
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Description");
            myInvoice[index].setDescription(input);
        
            do
            {
                input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) + " Required");
                try
                {
                    number = Double.parseDouble(input);
                    myInvoice[index].setQuantity(number);
                }
                        
                catch (NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "You must enter an double");
                }
            }  
            while(input=="");
            
            do
            {
                input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Price");
                try
                {
                    number = Double.parseDouble(input);
                    myInvoice[index].setPrice(number);
                }
                        
                catch (NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "You must enter an double");
                }
            }  
            while(input=="");
        
        }    
        
        invoiceTotal = myInvoice[0].getTotal() + myInvoice[1].getTotal() + myInvoice[2].getTotal();
        
        JOptionPane.showMessageDialog(null, "Part "  + "Description " +  "Qty " +  "Price\n" +  
        " " + myInvoice[0].getPartNumber() + " " + myInvoice[0].getDescription() + " " +  
        myInvoice[0].getQuantity() + " " + myInvoice[0].getPrice() + "\n" + myInvoice[1].getPartNumber() + " " + myInvoice[1].getDescription() + " " +  
        myInvoice[1].getQuantity() + " " + myInvoice[1].getPrice() + "\n" +  myInvoice[2].getPartNumber() + " " + myInvoice[2].getDescription() + " " +  
        myInvoice[2].getQuantity() + " " + myInvoice[2].getPrice() + "\n" + "                  $" + invoiceTotal);
        
    }
}

User is offlineProfile CardPM
+Quote Post

kwikness
RE: Exception Handling
17 May, 2007 - 08:30 AM
Post #2

New D.I.C Head
*

Joined: 22 Jan, 2007
Posts: 14


My Contributions
Include ur import statements and additional classes needed for compilation next time.
Also, I have no idea what the hell you're asking, lol.

But looking at your code, it seems that your loops are set to terminate only when the (input != "").

This means that if the user has entered anything at all, the loop will terminate.

This post has been edited by kwikness: 17 May, 2007 - 08:36 AM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Exception Handling
17 May, 2007 - 10:16 AM
Post #3

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

Bit tricky without all the source code, but a general answer would be to create a boolean value and set it to false. Include your InputDialog in a while loop that says while your boolean == false, then put your code, and inside your try {} say if your double number does not equal whatever you initialise it as then change the boolean to true, and you'll exit your loop, otherwise it will prompt again.

Hope that makes sense crazy.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month