overriding an error messaage

hi.

im building a airplane ticket machine.

basically when i test the GUI the error messages are wrong! EG: i open the GUI and dont enter any values, and then press issue single ticket, the "Number of Passengers must be greater than zero" error message comes up which is correct (at which time i want the cursor to focus on the passenger text box), the departure date error message follows it and when i click OK the focus is on the departure text box and not the passenger textbox.

basically i dont want the 2nd error message to appear if no values are entered, i just want the first error message to appear!....code is below....any help or links to some tutorials will be great!!....thanks guys

//create an exception handler for the passengers textBox

publicvoid numberMessage()

{

try

{

numberOfPassengers = Integer.parseInt(passengerText.getText());

if(numberOfPassengers <= 0)

{

JOptionPane.showMessageDialog(null,"Number of Passengers must be greater than zero","Error Message",JOptionPane.ERROR_MESSAGE);

this.passengerText.setText("");

this.passengerText.requestFocus();

}

}

catch (NumberFormatException ex)

{

JOptionPane.showMessageDialog(null,"Enter the number of Passengers required","Error Message",JOptionPane.ERROR_MESSAGE);

this.passengerText.setText("");

this.passengerText.requestFocus();

}

}

//set the method to create a single ticket

//create an instance of object ticket and call the methods from the class

publicvoid singleTicket()

{

String departure = departureText.getText();

String returns = returnText.getText();

if(departure.length() == 0 || returns.length() != 0)

{

JOptionPane.showMessageDialog(null,"Only Enter the departure date","Error Message", JOptionPane.ERROR_MESSAGE);

departureText.setText("");

returnText.setText("");

departureText.requestFocus();

}

elseif(numberOfPassengers > 0)

{

ticket =new Ticket();

ticket.setFrom((String)from.getSelectedItem());

ticket.setDestination((String)destination.getSelectedItem());

ticket.setClass((String)classes.getSelectedItem());

ticket.setPassengers(numberOfPassengers = Integer.parseInt(passengerText.getText()));

ticket.setDeparture(departureText.getText());

//ticket.setReturn(returnText.getText());

ticket.setTicketPrice();

ticket.printDetails();

//tickets.add(ticket); //add at later date, must add to tickets -- iterate through!!

}

}

[3767 byte] By [rich79a] at [2007-10-3 5:25:01]
# 1
You can check the input String's length for being 0 before trying to parse it. If length is 0, no input was provided. Otherwise it either works or is invalid or non-positive.
CeciNEstPasUnProgrammeura at 2007-7-14 23:32:13 > top of Java-index,Java Essentials,Java Programming...
# 2

after i check that the length is zero where do i call the method i created.

cause if i just call it in the single ticket method exactly the same thing happens again, except now i have three error messages on after the other...

public void passenger()

{

String passenger = passengerText.getText();

if(passenger.length() <= 0)

{

JOptionPane.showMessageDialog(null,"Number of Passengers must be greater than zero", "Error Message",JOptionPane.ERROR_MESSAGE);

this.passengerText.requestFocus();

}

}

rich79a at 2007-7-14 23:32:13 > top of Java-index,Java Essentials,Java Programming...
# 3

> after i check that the length is zero where do i call

> the method i created.

You're supposed to check in your oddly-named passenger() method, after you fetched the input using getText(). Just before you parse the String.

Grab a pencil and some paper and write up in case of which input you need to do what. That'll help you.

CeciNEstPasUnProgrammeura at 2007-7-14 23:32:13 > top of Java-index,Java Essentials,Java Programming...
# 4
Once you have an error in that method then just return.Problem solved.
cotton.ma at 2007-7-14 23:32:13 > top of Java-index,Java Essentials,Java Programming...
# 5
thanks...note...just quickly made the passenger method to test if it works...
rich79a at 2007-7-14 23:32:13 > top of Java-index,Java Essentials,Java Programming...