10 Replies - 806 Views - Last Post: 18 July 2011 - 06:51 PM Rate Topic: -----

#1 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Using Control Statements if/else when there is no user input

Posted 14 July 2011 - 09:33 PM

Hello,

I am working on a personal project. It is an L-Band Converter used to convert Satellite Downlink Frequencies from C or Ku-Band to a useable L-Band recognized by receivers. The formula is a simple subtraction formula used for either C-Band or Ku-Band. The problem that I am having is constructing the proper control statement using if/else to allow the user to choose which field he wants to enter the frequency into without having to enter a "0" as a placeholder in the other field in order to calculate the L-Band Frequency.

Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;





	public class LBandConverter extends JFrame 
	{
	private JTextField LBandConvertertf,cbandtf,kubandtf, lbandtf;
	private JButton calculateButton, exitButton;
	private JLabel LBandConverterLabel,cbandlabel,kubandlabel,lbandlabel;
	int cbandconstant = 5150;
	int kubandconstant = 10750;
	int frequency;
	
	// Button Handlers
	private calculateButtonHandler cbhandler;
	private exitButtonHandler ebhandler;
	
	
	public LBandConverter()
	{
	setTitle("Chris' L-Band Converter");
	setSize (300, 300);
	cbandlabel = new JLabel ("C-BAND Frequency");
	kubandlabel = new JLabel ("KU-BAND Frequency");
	cbandtf = new JTextField (10);
	kubandtf = new JTextField (10);
	lbandtf = new JTextField (10);
	
	//Specify Handlers For Each Button and Add Action Listeners
	calculateButton = new JButton ("Calculate L-Band");
	cbhandler = new calculateButtonHandler();
	calculateButton.addActionListener(cbhandler);
	exitButton =  new JButton ("Exit");
	ebhandler = new exitButtonHandler();
	exitButton.addActionListener(ebhandler);
		
	
	add (cbandlabel);
	add (cbandtf);
	add (kubandlabel);
	add (kubandtf);
	add (calculateButton);
	add (lbandtf);
	add (exitButton);
	
//	set the layout manager
	setLayout(new FlowLayout());
	
	setVisible(true);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	private class calculateButtonHandler implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			 
			int   cbandfrequency, kubandfrequency = 0  , lbandfrequency = 0;
			
			
						 		 
			 	cbandfrequency = Integer.parseInt(cbandtf.getText());
			 	kubandfrequency = Integer.parseInt(kubandtf.getText());
			 
			 if (cbandfrequency == 0)
			  
				 lbandfrequency = kubandfrequency-10750;
			 
			 		 
			 else if (kubandfrequency == 0) 		 
															
			 	lbandfrequency = 5150 - cbandfrequency;
			 
			 	lbandtf.setText("" + lbandfrequency);
				 
			 
		}
	}
	public class exitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
		
	public static void main(String[] args) 
	
	{
		
		LBandConverter lbc = new LBandConverter ();

	}

}
	



I have tried various combinations of if/else statements but this is the best I have come up with so far. The program works only if both fields have a value i.e. C-Band Frequency = 4200 and Ku-Band Frequency = 0. When calculated, the L-Band = 950. I want the user to choose only to enter 1 frequency either C-Band or Ku and get the resulting L-Band.

Thank you,

Chris DeCamp

This post has been edited by n8wxs: 14 July 2011 - 10:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Control Statements if/else when there is no user input

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Using Control Statements if/else when there is no user input

Posted 14 July 2011 - 11:21 PM

Simply process the text field with a non zero text length
Was This Post Helpful? 1
  • +
  • -

#3 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 01:35 PM

View PostChrisDeCamp, on 14 July 2011 - 08:33 PM, said:

Hello,

I am working on a personal project. It is an L-Band Converter used to convert Satellite Downlink Frequencies from C or Ku-Band to a useable L-Band recognized by receivers. The formula is a simple subtraction formula used for either C-Band or Ku-Band. The problem that I am having is constructing the proper control statement using if/else to allow the user to choose which field he wants to enter the frequency into without having to enter a "0" as a placeholder in the other field in order to calculate the L-Band Frequency.

Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;





	public class LBandConverter extends JFrame 
	{
	private JTextField LBandConvertertf,cbandtf,kubandtf, lbandtf;
	private JButton calculateButton, exitButton;
	private JLabel LBandConverterLabel,cbandlabel,kubandlabel,lbandlabel;
	int cbandconstant = 5150;
	int kubandconstant = 10750;
	int frequency;
	
	// Button Handlers
	private calculateButtonHandler cbhandler;
	private exitButtonHandler ebhandler;
	
	
	public LBandConverter()
	{
	setTitle("Chris' L-Band Converter");
	setSize (300, 300);
	cbandlabel = new JLabel ("C-BAND Frequency");
	kubandlabel = new JLabel ("KU-BAND Frequency");
	cbandtf = new JTextField (10);
	kubandtf = new JTextField (10);
	lbandtf = new JTextField (10);
	
	//Specify Handlers For Each Button and Add Action Listeners
	calculateButton = new JButton ("Calculate L-Band");
	cbhandler = new calculateButtonHandler();
	calculateButton.addActionListener(cbhandler);
	exitButton =  new JButton ("Exit");
	ebhandler = new exitButtonHandler();
	exitButton.addActionListener(ebhandler);
		
	
	add (cbandlabel);
	add (cbandtf);
	add (kubandlabel);
	add (kubandtf);
	add (calculateButton);
	add (lbandtf);
	add (exitButton);
	
//	set the layout manager
	setLayout(new FlowLayout());
	
	setVisible(true);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	private class calculateButtonHandler implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			 
			int   cbandfrequency, kubandfrequency = 0  , lbandfrequency = 0;
			
			
						 		 
			 	cbandfrequency = Integer.parseInt(cbandtf.getText());
			 	kubandfrequency = Integer.parseInt(kubandtf.getText());
			 
			 if (cbandfrequency == 0)
			  
				 lbandfrequency = kubandfrequency-10750;
			 
			 		 
			 else if (kubandfrequency == 0) 		 
															
			 	lbandfrequency = 5150 - cbandfrequency;
			 
			 	lbandtf.setText("" + lbandfrequency);
				 
			 
		}
	}
	public class exitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
		
	public static void main(String[] args) 
	
	{
		
		LBandConverter lbc = new LBandConverter ();

	}

}
	



I have tried various combinations of if/else statements but this is the best I have come up with so far. The program works only if both fields have a value i.e. C-Band Frequency = 4200 and Ku-Band Frequency = 0. When calculated, the L-Band = 950. I want the user to choose only to enter 1 frequency either C-Band or Ku and get the resulting L-Band.

Thank you,

Chris DeCamp

Was This Post Helpful? 0
  • +
  • -

#4 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 01:44 PM

Hello n8wxs,

Thank you for your response!

I tried processing the text field with a non-zero length as you recommended
cbandtf = new JTextField (5);
but that just made the text field smaller. If this is not correct, could you please provide an example?

Thank you,

Chris DeCamp
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 02:53 PM

I think n8wxs meant to test the fields and only process those with contents greater than zero length. For example, if you have 3 fields, 1 with data and 2 without, test all 3 fields for their contents and only process the fields that contain data greater than zero length or that are non null.
Was This Post Helpful? 1
  • +
  • -

#6 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 04:12 PM

View PostGregBrannon, on 15 July 2011 - 01:53 PM, said:

I think n8wxs meant to test the fields and only process those with contents greater than zero length. For example, if you have 3 fields, 1 with data and 2 without, test all 3 fields for their contents and only process the fields that contain data greater than zero length or that are non null.


Hello Greg!

I've tried different combinations of:
if (cbandfrequency > 0)
			  
				lbandfrequency = kubandfrequency-10750;
			 
			 		 
			 else if (kubandfrequency > 0) 		 
															
			 	lbandfrequency = 5150 - cbandfrequency;
			 


...but I am still not getting the results I want. Can you please elaborate?

Thank you,

Chris DeCamp
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 04:32 PM

You have 3 JTextFields for your frequency parameters, cbandtf, kubandtf, and lbandtf. As I understand your question, you want to only process the TextField that has data in it and ignore the TextFields that do not have data. You can test each of the TextFields for their contents using something like:

if ( !cbandtf.getText.equals( "" ) )
{
    // code for cbandtf here
}
else if ( !kubandtf.getText.equals( "" ) )
{
    // code for kubandtf here    
}
else if ( !lbandtf.getText.equals( "" ) )
{
    // code for lbandtf here
}
// you might include a default error message here



You could also set all TextFields to zero (or some default value) and only process the text field that does not contain the default value. There are many ways to accomplish this.

If I misunderstand what you're trying to do, please explain again.
Was This Post Helpful? 1
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Using Control Statements if/else when there is no user input

Posted 15 July 2011 - 05:35 PM

Something like this:

...
    private class calculateButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            int cbandfrequency, kubandfrequency = 0, lbandfrequency = 0;

            if (cbandtf.getText().length() != 0) {
                cbandfrequency = Integer.parseInt(cbandtf.getText());
                lbandfrequency = 5150 - cbandfrequency;
            } else if (kubandtf.getText().length() != 0) {
                kubandfrequency = Integer.parseInt(kubandtf.getText());
                lbandfrequency = kubandfrequency - 10750;
            }


            lbandtf.setText("" + lbandfrequency);


        }
    }
...



You may wanna check that only one of the text fields has a value and not display the output if both frequencies have been entered
Was This Post Helpful? 1
  • +
  • -

#9 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 17 July 2011 - 03:30 PM

View Postn8wxs, on 15 July 2011 - 04:35 PM, said:

Something like this:

...
    private class calculateButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            int cbandfrequency, kubandfrequency = 0, lbandfrequency = 0;

            if (cbandtf.getText().length() != 0) {
                cbandfrequency = Integer.parseInt(cbandtf.getText());
                lbandfrequency = 5150 - cbandfrequency;
            } else if (kubandtf.getText().length() != 0) {
                kubandfrequency = Integer.parseInt(kubandtf.getText());
                lbandfrequency = kubandfrequency - 10750;
            }


            lbandtf.setText("" + lbandfrequency);


        }
    }
...



You may wanna check that only one of the text fields has a value and not display the output if both frequencies have been entered

Was This Post Helpful? 0
  • +
  • -

#10 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 17 July 2011 - 03:54 PM

Hello n8wsx & Greg,

Thank you both for your input on this.I'm sorry that I haven't replied sooner. The weekend activites have distracted me. Both of your recommendations have helped me quite a lot! The formula that seems to work best so far is:

 int   cbandfrequency=0, kubandfrequency = 0, lbandfrequency = 0;
			
			 
			 cbandfrequency = Integer.parseInt(cbandtf.getText());
			 
					
			 			 
				if (cbandtf.getText().length()!= 0){
					cbandfrequency = Integer.parseInt(cbandtf.getText());
					lbandfrequency = 5150 - cbandfrequency;
				
				} else if (kubandtf.getText().length()!= 0){
					kubandfrequency = Integer.parseInt(kubandtf.getText());
					lbandfrequency = kubandfrequency - 10750;
									
				}

					
			lbandtf.setText("" + lbandfrequency);


However, it only seems to work only when the user inputs C-Band Frequency and not with Ku-band. In other words, when I enter C-Band frequency it calculates the proper L-Band. When I enter in the Ku-Band frequency, I am still getting errors such as:

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


I'm not sure what that means. I will have to play around with it and see if I can figure it out.

Thank you both again!,

ChrisDeCamp

View PostGregBrannon, on 15 July 2011 - 03:32 PM, said:

You have 3 JTextFields for your frequency parameters, cbandtf, kubandtf, and lbandtf. As I understand your question, you want to only process the TextField that has data in it and ignore the TextFields that do not have data. You can test each of the TextFields for their contents using something like:

if ( !cbandtf.getText.equals( "" ) )
{
    // code for cbandtf here
}
else if ( !kubandtf.getText.equals( "" ) )
{
    // code for kubandtf here    
}
else if ( !lbandtf.getText.equals( "" ) )
{
    // code for lbandtf here
}
// you might include a default error message here



You could also set all TextFields to zero (or some default value) and only process the text field that does not contain the default value. There are many ways to accomplish this.

If I misunderstand what you're trying to do, please explain again.


Hello n8wsx & Greg,

Thank you both for your input on this. I'm sorry that I haven't replied sooner. The weekend activites have distracted me. Both of your recommendations have helped me quite a lot! The formula that seems to work best so far is:

int   cbandfrequency=0, kubandfrequency = 0, lbandfrequency = 0;  

02               

03                

04             cbandfrequency = Integer.parseInt(cbandtf.getText());  

05                

06                       

07                            

08                if (cbandtf.getText().length()!= 0){  

09                    cbandfrequency = Integer.parseInt(cbandtf.getText());  

10                    lbandfrequency = 5150 - cbandfrequency;  

11                   

12                } else if (kubandtf.getText().length()!= 0){  

13                    kubandfrequency = Integer.parseInt(kubandtf.getText());  

14                    lbandfrequency = kubandfrequency - 10750;  

15                                       

16                }  

17   

18                       

19            lbandtf.setText("" + lbandfrequency); 




However, it only seems to work only when the user inputs C-Band Frequency and not with Ku-band. In other words, when I enter C-Band frequency it calculates the proper L-Band. When I enter in the Ku-Band frequency, I am still getting errors such as:

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  

2     at java.awt.EventDispatchThread.run(Unknown Source) 



I'm not sure what that means. I will have to play around with it and see if I can figure it out.

Thank you both again!,

ChrisDeCamp
Was This Post Helpful? 0
  • +
  • -

#11 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: Using Control Statements if/else when there is no user input

Posted 18 July 2011 - 06:51 PM

Hello n8wxs and Greg,

I finally got it to work!

Thanks again for both of your valuable input!

ChrisDeCamp
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1