Web Design and Web Development Forum

  1. #1
    Join Date
    Jan 2005
    Posts
    29
    Rep Power
    0
  2. melissa84 is on a distinguished road
  3. radio buttons

    Hi all!

    I'm making a temperature conversion program, and I'm using radio buttons so the user can select from what temperature to what temperature to convert their number. I've coded an input field key handler so that when a character other than a number is entered, the program will tell the user and backspace the character. But the program doesn't know how to handle a radio button being selected without a character entered.

    Here's the code for the radio button handler (I'm not sure if it will be of any help or not):
    Code:
    class RButHandler implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent e)
    		{
    			//formats the result temperature to two decimal places
    			DecimalFormat num = new DecimalFormat(",###.##");
    			//data declaration section
    			String instring;
    			double temp, outvalue;
    			
    			if (e.getSource() == rdbutFtoC) //if FtoC button selected
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = 5.0/9.0 * (temp - 32); //formula for F to C conversion 
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Celsius.");
    			}
    			else if (e.getSource() == rdbutCtoF)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = (9.0/5.0 * temp) + 32; //formula for C to F conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Farenheit.");
    			}
    			else if (e.getSource() == rdbutFtoK)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = 5.0/9.0 * (temp - 32) + 273; //formula for F to K conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Kelvin.");
    			}
    			else if (e.getSource() == rdbutCtoK)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = temp + 273; //formula for C to K conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Kelvin.");
    			}
    			
    		}
    	}
    Thanks!
    Melissa
    Last edited by 4fingers; 12-01-2005 at 10:35 AM. Reason: Added in CODE Tags
    Reply With Quote Reply With Quote
  4. #2
    4fingers's Avatar
    Join Date
    Aug 2005
    Location
    UK, Scotland
    Age
    25
    Posts
    850
    Rep Power
    7
  5. 4fingers is on a distinguished road
  6. Re: radio buttons

    Hi Melissa,
    So if I get you correctly Everything seems to work fine except when there is nothing in the TextField and you click between the radio buttons.

    A simple way of solving this would be if there is no data in the TextField make it so none of the calculations are carried out.

    One way of doing that is simply add
    Code:
    && enterTempField.getText().length() != 0
    To each of the IF statments. That way it checks to see if the length of the TextField is greater than 0 otherwise it wont carry out any of the calcuations.

    So try this for example
    Code:
    class RButHandler implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent e)
    		{
    			//formats the result temperature to two decimal places
    			DecimalFormat num = new DecimalFormat(",###.##");
    			//data declaration section
    			String instring;
    			double temp, outvalue;
    			
    			if (e.getSource() == rdbutFtoC && enterTempField.getText().length() != 0) //if FtoC button selected
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = 5.0/9.0 * (temp - 32); //formula for F to C conversion 
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Celsius.");
    			}
    			else if (e.getSource() == rdbutCtoF && enterTempField.getText().length() != 0)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = (9.0/5.0 * temp) + 32; //formula for C to F conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Farenheit.");
    			}
    			else if (e.getSource() == rdbutFtoK && enterTempField.getText().length() != 0)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = 5.0/9.0 * (temp - 32) + 273; //formula for F to K conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Kelvin.");
    			}
    			else if (e.getSource() == rdbutCtoK && enterTempField.getText().length() != 0)
    			{	
    				instring = enterTempField.getText(); //reads the input into the textfield
    				temp = Double.parseDouble(instring); //converts the text to a double
    				outvalue = temp + 273; //formula for C to K conversion
    				//make answer appear in answer text field
    				answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Kelvin.");
    			}
    			
    		}
    	}
    There are many other possible ways of doing this like enclosing all of the event handling data in one big IF or instead using something like enterTempField.getText().equalsIgnoreCase("") or enterTempField.getText().matches() and use regular expression to see if the TextField is empty.

    Either Way I hope that solves your problem
    Last edited by 4fingers; 12-01-2005 at 03:31 PM.

    The four finger rule: There is no middle finger.
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Jan 2005
    Posts
    29
    Rep Power
    0
  8. melissa84 is on a distinguished road
  9. Re: radio buttons

    Thanks for your help!
    Melissa
    Reply With Quote Reply With Quote

Similar Threads

  1. Internet Radio Pros Best In The World!
    By internetradiopros in forum Chit Chat and Hangout
    Replies: 0
    Last Post: 08-13-2005, 01:00 AM
  2. BBC Radio 1
    By YoungCoder in forum Chit Chat and Hangout
    Replies: 8
    Last Post: 08-12-2005, 04:50 PM
  3. AM/FM Radio?
    By boochy in forum Chit Chat and Hangout
    Replies: 21
    Last Post: 07-12-2005, 10:15 AM
  4. radio
    By Cyber_Syrnyk in forum Chit Chat and Hangout
    Replies: 38
    Last Post: 06-16-2005, 11:44 AM