#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Radio button help


    I am having trouble finishing my loop for a project I am working for class. The project requires me to convert temperature to F, C, and K. I have the majority of the code, but I cannot finish the last loop for my radio button. Also, my code is giving an error, saying that I am missing a semicolon, but I cannot find where.
    This is what I have:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.text.*;
    
    public class ConvertFrame extends JFrame
    {
    	private JPanel convertFrom;
    	private JPanel convertTo;
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JLabel label4;
    	private JTextField temperature1, temperature2;
    	private ButtonGroup radioFrom, radioTo;
    	private JRadioButton CelciusBoxTo; 
    	private JRadioButton FahrenheitBoxTo;
    	private JRadioButton KelvinBoxTo;
    	private JRadioButton CelciusBoxFrom;
    	private JRadioButton FahrenheitBoxFrom;
    	private JRadioButton KelvinBoxFrom;
    	DecimalFormat three = new DecimalFormat("0.000");
       
        public ConvertFrame()
        {
        	super("Temperature Converter");
        	FahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
        	CelciusBoxFrom = new JRadioButton( "Celcius", false );
        	KelvinBoxFrom = new JRadioButton( "Kelvin", false );
        	radioFrom = new ButtonGroup();
        	radioFrom.add( FahrenheitBoxFrom );
        	radioFrom.add( CelciusBoxFrom );
        	radioFrom.add( KelvinBoxFrom );
        	FahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
        	CelciusBoxTo = new JRadioButton( "Celcius", true );
        	KelvinBoxTo = new JRadioButton( "Kelvin", false );
        	radioTo = new ButtonGroup();
        	radioTo.add( FahrenheitBoxTo );
        	radioTo.add( CelciusBoxTo );
        	radioTo.add( KelvinBoxTo );
        	convertFrom = new JPanel();
        	convertFrom.setLayout( new GridLayout( 1, 3 ) );
        	convertFrom.add( FahrenheitBoxFrom );
        	convertFrom.add( CelciusBoxFrom );
        	convertFrom.add( KelvinBoxFrom );
        	convertTo = new JPanel();
        	convertTo.setLayout( new GridLayout( 1, 3 ) );
        	convertTo.add( FahrenheitBoxTo );
        	convertTo.add( CelciusBoxTo );
        	convertTo.add( KelvinBoxTo );
        	label1 = new JLabel( "Convert from:" );
        	label2 = new JLabel( "Convert to:" );
        	label3 = new JLabel( "Enter Numeric Temperature: " );
        	label4 = new JLabel( "Comparable Temperature is: " );
        	temperature1 = new JTextField( 10 );
        	temperature1.addActionListener(
        	new ActionListener() 
        	{ 
        	// perform conversions
        	public void actionPerformed( ActionEvent event )
        	{
        	int convertTemp, temp;
        	temp = Integer.parseInt( ( ( JTextField )
        	event.getSource() ).getText() );
        	// fahrenheit to celcius
        	if ( FahrenheitBoxFrom.isSelected() &&
        	CelciusBoxTo.isSelected() ) 
        	{
        	convertTemp = ( int ) ((5.0 / 9.0)*(temp-32));
        	temperature2.setText( String.valueOf( convertTemp ) );
        	}
        	// fahrenheit to kelvin
        	else if ( FahrenheitBoxFrom.isSelected() &&
        	KelvinBoxTo.isSelected() ) 
        	{
        	convertTemp = ( int )
        	((5.0 / 9.0)*(temp - 32) + 273);
        	temperature2.setText( String.valueOf( convertTemp ) );
        	}
        	// celcius to fahrenheit
        	else if ( CelciusBoxFrom.isSelected() &&
        	FahrenheitBoxTo.isSelected() ) 
        	{
        	convertTemp = ( int ) ((9.0 / 5.0)*(temp + 32));
        	temperature2.setText( String.valueOf( convertTemp ) );
        	}
        	// celcius to kelvin
        	else (CelciusBoxFrom.isSelected() && 
        	KelvinBoxTo.isSelected())
        	{
        	convertTemp = (temp + 273);
        	temperature2.setText( String.valueOf( convertTemp ) );
        	}
        	}
        	}
        }
        
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Code:
    else (CelciusBoxFrom.isSelected() && 
        	KelvinBoxTo.isSelected())
        	{
        	convertTemp = (int)(temp + 273);
        	temperature2.setText( String.valueOf( convertTemp ) );
        	}
    This is the part that is giving me errors, not sure why it is.
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    you're missing one keyword
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Oh, I forgot to update this. I'm trying to put some finishing touches on my program. Trying to refine it a bit more to what my project is suppose to look like, with the text placement etc.

    Code:
    // Program converts temperatures.
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    import javax.swing.*;
    
    public class ConvertFrame extends JFrame {
    private JPanel convertFrom, convertTo;
    private JLabel label1, label2, label3, label4;
    private JTextField temperature1, temperature2;
    private ButtonGroup radioFrom, radioTo;
    private JRadioButton celciusBoxTo, fahrenheitBoxTo,
    kelvinBoxTo, celciusBoxFrom, fahrenheitBoxFrom, kelvinBoxFrom;
    
    
    
    // set up GUI
    public ConvertFrame()
    {
    super( "Temperature Conversion" );
    
    fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
    celciusBoxFrom = new JRadioButton( "Celcius", false );
    kelvinBoxFrom = new JRadioButton( "Kelvin", false );
    radioFrom = new ButtonGroup();
    radioFrom.add( fahrenheitBoxFrom );
    radioFrom.add( celciusBoxFrom );
    radioFrom.add( kelvinBoxFrom );
    
    fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
    celciusBoxTo = new JRadioButton( "Celcius", true );
    kelvinBoxTo = new JRadioButton( "Kelvin", false );
    radioTo = new ButtonGroup();
    radioTo.add( fahrenheitBoxTo );
    radioTo.add( celciusBoxTo );
    radioTo.add( kelvinBoxTo );
    
    convertFrom = new JPanel();
    convertFrom.setLayout( new GridLayout( 3, 1 ) );
    convertFrom.add( fahrenheitBoxFrom );
    convertFrom.add( celciusBoxFrom );
    convertFrom.add( kelvinBoxFrom );
    
    convertTo = new JPanel();
    convertTo.setLayout( new GridLayout( 3, 1 ) );
    convertTo.add( fahrenheitBoxTo );
    convertTo.add( celciusBoxTo );
    convertTo.add( kelvinBoxTo );
    
    label1 = new JLabel( "Input Scale: " );
    label2 = new JLabel( "Output Scale: " );
    label3 = new JLabel( "Input: " );
    label4 = new JLabel( "Output: " );
    
    temperature1 = new JTextField( 10 );
    temperature1.addActionListener(
    
    new ActionListener() { // anonymous inner class
    
    // perform conversions
    public void actionPerformed( ActionEvent event )
    {
    double convertTemp, temp;
    DecimalFormat df = new DecimalFormat("0.00");
    
    
    temp = Integer.parseInt( ( ( JTextField )
    event.getSource() ).getText() );
    
    // fahrenheit to celcius
    if ( fahrenheitBoxFrom.isSelected() &&
    celciusBoxTo.isSelected() ) {
    convertTemp = ( double ) ( 5.0 / 9.0 * ( temp - 32 ) );
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    // fahrenheit to kelvin
    else if ( fahrenheitBoxFrom.isSelected() &&
    kelvinBoxTo.isSelected() ) {
    convertTemp = ( double )
    ( 5.0 / 9.0 * ( temp - 32 ) + 273 );
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    
    // celcius to fahrenheit
    else if ( celciusBoxFrom.isSelected() &&
    fahrenheitBoxTo.isSelected() ) {
    convertTemp = ( double ) ( 9.0 / 5.0 * temp + 32 );
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    
    // celcius to kelvin
    else if ( celciusBoxFrom.isSelected() &&
    kelvinBoxTo.isSelected() ) {
    convertTemp = temp + 273;
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    
    // kelvin to celcius
    else if ( kelvinBoxFrom.isSelected() &&
    celciusBoxTo.isSelected() ) {
    convertTemp = temp - 273;
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    
    // kelvin to fahrenheit
    else if ( kelvinBoxFrom.isSelected() &&
    fahrenheitBoxTo.isSelected() ) {
    convertTemp = ( double ) ( 5.0 /9.0 * ( temp - 273 ) + 32 );
    temperature2.setText( String.valueOf( convertTemp ) );
    temperature2.setText(" "+ df.format(convertTemp));
    }
    
    } // end method actionPerformed
    
    } // end anonymous inner class
    
    ); // end call to addActionListener
    
    temperature2 = new JTextField( 10 );
    temperature2.setEditable( false );
    
    // add components to GUI
    Container container = getContentPane();
    container.setLayout( new GridLayout( 4, 1 ) );
    container.add( label1 );
    container.add( convertFrom );
    container.add( label3 );
    container.add( temperature1 );
    container.add( label2 );
    container.add( convertTo );
    container.add( label4 );
    container.add( temperature2 );
    
    
    setSize( 300, 200 );
    setVisible( true );
    
    }} // end constructor
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0


    That's what the finished program should look like.



    This is what I have.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Code:
    temperature2.setText(" "+ df.format(convertTemp))
    What would I need to add to this code to make it display F, C, K? Right now it will only show the conversion up to 2 decimal places.

IMN logo majestic logo threadwatch logo seochat tools logo