Problem with radio button loop in temperature conversion program.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1455 Views - Last Post: 07 August 2013 - 09:58 AM Rate Topic: -----

#1 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:25 AM

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.

This is my 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.0f / 9.0f * ( 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 ) );
    	}
    	}
    	}
    }
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with radio button loop in temperature conversion program.

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:37 AM

Quote

but I cannot finish the last loop for my radio button.
... meaning ..?
Was This Post Helpful? 0
  • +
  • -

#3 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:39 AM

else (CelciusBoxFrom.isSelected() && 
    	KelvinBoxTo.isSelected())
    	{
    	convertTemp = (int)(temp + 273);
    	temperature2.setText( String.valueOf( convertTemp ) );
    	}


This portion of my code is giving me errors, and I'm not sure what I'm doing wrong.
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:45 AM

View PostDubyer, on 01 August 2013 - 04:39 PM, said:

else (CelciusBoxFrom.isSelected() && 
    	KelvinBoxTo.isSelected())
    	{
    	convertTemp = (int)(temp + 273);
    	temperature2.setText( String.valueOf( convertTemp ) );
    	}


This portion of my code is giving me errors, and I'm not sure what I'm doing wrong.


You're missing an if in that statement. Also on line 96 you need to close the parenthesis and add a semi-colon.
Was This Post Helpful? 0
  • +
  • -

#5 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:51 AM

I add the the if part in the statement, but even after adding a semi colon to line 96, I still have an error.

I added the if part in the statement, but even after adding a semi colon to line 96, I still have an error.
Was This Post Helpful? 0
  • +
  • -

#6 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:53 AM

Line 96 should look like this:

});



Can you tell us what the error says?
Was This Post Helpful? 0
  • +
  • -

#7 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 09:56 AM

else if (CelciusBoxFrom.isSelected() && 
    	KelvinBoxTo.isSelected())
    	{
    	convertTemp = (int)(temp + 273);
    	temperature2.setText( String.valueOf( convertTemp ) );
    	}
    	}
    ;}
    }
    
}


Oh, thank you.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 12:56 PM

You'll find your code a lot easier to write and read if you avoid the use of anonymous inner classes
Was This Post Helpful? 0
  • +
  • -

#9 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 01:10 PM

So I have been playing around with it some more after my first couple of posts. I got it to run, but it's not displaying an output. Also can someone can help me do some tweaking so it looks like the attached?

// Program converts temperatures.
import java.awt.*;
import java.awt.event.*;
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 )
{
int convertTemp, temp;

temp = Integer.parseInt( ( ( JTextField )
event.getSource() ).getText() );

// fahrenheit to celcius
if ( fahrenheitBoxFrom.isSelected() &&
celciusBoxTo.isSelected() ) {
convertTemp = ( int ) ( 5.0F / 9.0F * ( temp - 32 ) );
temperature2.setText( String.valueOf( convertTemp ) );
}
// fahrenheit to kelvin
else if ( fahrenheitBoxFrom.isSelected() &&
kelvinBoxTo.isSelected() ) {
convertTemp = ( int )
( 5.0F / 9.0F * ( temp - 32 ) + 273 );
temperature2.setText( String.valueOf( convertTemp ) );
}

// celcius to fahrenheit
else if ( celciusBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected() ) {
convertTemp = ( int ) ( 9.0F / 5.0F * temp + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
}

// celcius to kelvin
else if ( celciusBoxFrom.isSelected() &&
kelvinBoxTo.isSelected() ) {
convertTemp = temp + 273;
temperature2.setText( String.valueOf( convertTemp ) );
}

// kelvin to celcius
else if ( kelvinBoxFrom.isSelected() &&
celciusBoxTo.isSelected() ) {
convertTemp = temp - 273;
temperature2.setText( String.valueOf( convertTemp ) );
}

// kelvin to fahrenheit
else if ( kelvinBoxFrom.isSelected() &&
fahrenheitBoxTo.isSelected() ) {
convertTemp = ( int ) ( 5.0F /9.0F * ( temp - 273 ) + 32 );
temperature2.setText( String.valueOf( 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( 5, 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( 400, 250 );
setVisible( true );

}} // end constructor




This is what mine looks like currently.

The left one is what it should look like, and the right is what I have.

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 01:19 PM

I have to use the anonymous inner class because that's what we learned in class, and my Professor wants us to do it that way, have to go by the book.
Was This Post Helpful? 0
  • +
  • -

#11 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 01:24 PM

I tweaked it a bit myself, but I still can't get it to display properly, even after adding the decimal import into my code.

import java.text.DecimalFormat;
DecimalFormat three = new DecimalFormat("0.000");



What my frame looks like now.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 01:45 PM

I got it to display two decimal places. Just trying to get it to display " F" now.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#13 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 02:07 PM

temperature2.setText(" "+ df.format(convertTemp))


What would I need to add to this code to make it display F, C, K?
Was This Post Helpful? 0
  • +
  • -

#14 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 02:49 PM

View PostDubyer, on 01 August 2013 - 09:07 PM, said:

temperature2.setText(" "+ df.format(convertTemp))


What would I need to add to this code to make it display F, C, K?


You'll need a boolean for if it's F, C, or K and something along the lines of,

if(f)
    ...setText(... + "F");
else if(c)
    ...setText(... + "C");
else
    ...setText(... + "K");   


Was This Post Helpful? 0
  • +
  • -

#15 Dubyer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 13-July 13

Re: Problem with radio button loop in temperature conversion program.

Posted 01 August 2013 - 04:11 PM

I know the character value for the degree symbol is 176. How can I output a degree symbol into a formatted string?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3