6 Replies - 580 Views - Last Post: 01 July 2011 - 06:57 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Nested if statements

Posted 01 July 2011 - 01:53 AM

I have this code with me which contains two radio buttons. i want the user to choose one of the radio buttons first,then the user can proceed on filling up the empty textfields. Then when the user presses the submit button, a dialog box should pop up with the user's info.If incase a user has not clicke don one of the radio buttons and pressed the submit button,then a dialog box pops up alerting him about it.Now the problem is that even if when i choose one of the radio buttons
and fill up the textfield and press submit,the dialog box still pops up.

Now here's what i have done:


String MyID,firstname,lastname;
		if(e.getSource() == jrbStudent || e.getSource() == jrbJavaStudents)
        {		
		 if(e.getSource() ==  jbtSubmit)
		 {  
		    
			 
	            MyID =   (jtfFName.getText());
                   firstname =    (jtfID.getText());
                   lastname =     (jtfLName.getText()); 
		     
                  JOptionPane.showMessageDialog(null, MyID + firstname + lastname);

                   }
	  
       }  
        else 
		          JOptionPane.showMessageDialog(null,"Please click on one of the radiobuttons");	






Any ideas will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Nested if statements

#2 itengineer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-April 09

Re: Nested if statements

Posted 01 July 2011 - 02:34 AM

I think there is nothing wrong with the code. You have the conditions
if(e.getSource() == jrbStudent || e.getSource() == jrbJavaStudents)



so if the event is generated from some other source it will go to your else. There is no check if the textfields are filled or not.

Also please always post code appropriate code. This code does not show which action you are handling.

--itengineer
Was This Post Helpful? 0
  • +
  • -

#3 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Nested if statements

Posted 01 July 2011 - 02:39 AM

I guess jrbStudent and jrbJavaStudents are the RadioButtons? If so, I'm pretty sure the condition of the first if will never be true! By doing something like e.getSource() == jrbStudent you check whether the event was triggered by a certain Component. Since you want to submit the form by using a submit button, the first if is obsolete.
What you actually want to do is to check whether the RadioButtons were checked/selected. You can do so by using: jrbStudent.isSelected().
Furthermore, you should rethink the structure of the method you provided. Is there any chance that the event could have been triggered by anything else than the submit button? If not, you do not need to check for the source at all. Something like
String MyID;
String firstname;
String lastname;

if (!(jrbStudent.isSelected() || jrbJavaStudents.isSelected()) {
  JOptionPane.showMessageDialog(null,"Please click on one of the radiobuttons");
  return;
}
MyID =   (jtfFName.getText());
firstname =    (jtfID.getText());
lastname =     (jtfLName.getText()); 
JOptionPane.showMessageDialog(null, MyID + firstname + lastname);

is completely sufficient.
Was This Post Helpful? 2
  • +
  • -

#4 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Nested if statements

Posted 01 July 2011 - 09:06 AM

n8schatten,thank you very much for your reply.Your idea worked well and i understood it completely. Thanks man.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: Nested if statements

Posted 01 July 2011 - 10:08 AM

Quote

If incase a user has not clicke don one of the radio buttons



Make sure that doesn't happen by adding them to a ButtonGroup. imo radio buttons placed together should not be allowed to be all unchecked
Was This Post Helpful? 1
  • +
  • -

#6 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Nested if statements

Posted 01 July 2011 - 01:54 PM

Goose,nice point,thanks alot.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Nested if statements

Posted 01 July 2011 - 06:57 PM

String MyID,firstname,lastname;
	if(e.getSource() == jrbStudent || e.getSource() == jrbJavaStudents)
        {
         // if you enter this if it is because source is jrbStudent OR jrbJavaStudents
         // so surely cannot be jbtSumit as you check in the next statement	
		 if(e.getSource() ==  jbtSubmit)
		 {  
		    


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1