Celsius Fahrenheit converter GUI; problems with char vs int?

I think my problem is I am treating the char as if it were an int, you are probably supposed to do it a different way, but I am new to java, and I just don't know yet what I am supposed to do. Here is the assignment and the code. Any help/knowledge is greatly appreciated.

A Temperature class that has two parameters, a temperature value (floating-point) number and a character for the scale, either 'C' or 'F'.

Class will have four constructor methods, one for each instance variable (assume zero degrees if no value is specified and C if no scale is specified, one with two parameters for the two instance variables, and a default constructor (set to zero degrees Celsius).

Class will also have two accesor methods, one to return the temperature in C and one to return the temperature in F, using the formulas from the textbook, and round to the nearest tenth of a degree.

It will have three reset methods, one to set the value, one to set the scale ('F' or 'C') and one to set both.

It will have three comparison methods, one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another.

A suitable toString() method.

A driver program that tests all the methods.

Use each of the constructors, include at least one true and one false case for each of th comparison methods, and test at least the following temperature equalities: 0.0 degrees C = 32.0 degrees F, -40.0 degrees C = -40.0 degrees F, and 100.0 degrees C = 212 degrees F.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

publicclass Temp1extends JFrameimplements ActionListener{

private JTextField jtfNum, jtfCF, jtfResult, jtfResultCF;

private JButton jbtCelF, jbtFahC, jbtClear, jbtClearBoth, jbtClearNum, jbtClearCF, jbtClose;

privatefloat num1, num2;

privatechar cf;

publicstaticvoid main(String [] args){

Temp1 temp =new Temp1();

temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

temp.setSize(800,100);

temp.setTitle("Temperature C/F Converter");

temp.setVisible(true);

}

public Temp1(){

JPanel p1 =new JPanel();

p1.setLayout(new FlowLayout());

p1.add(new JLabel("Degrees : "));

p1.add(jtfNum =new JTextField(10));

p1.add(new JLabel("C/F : "));

p1.add(jtfCF =new JTextField(2));

p1.add(new JLabel("Result : "));

p1.add(jtfResult =new JTextField(10));

jtfResult.setEditable(false);

p1.add(jtfResult =new JTextField(2));

jtfResultCF.setEditable(false);

JPanel p2 =new JPanel();

p2.setLayout(new FlowLayout());

p2.add(jbtCelF =new JButton("Convert to Fahrenheit"));

p2.add(jbtFahC =new JButton("Convert to Celsius"));

p2.add(jbtClose =new JButton("Exit Program"));

p2.add(jbtClearNum =new JButton("Clear Degrees"));

p2.add(jbtClearCF =new JButton("Clear C/F"));

p2.add(jbtClearBoth =new JButton("Clear Degrees and C/F"));

p2.add(jbtClear =new JButton("Clear All"));

getContentPane().setLayout(new BorderLayout());

getContentPane().add(p1, BorderLayout.NORTH);

getContentPane().add(p2, BorderLayout.CENTER);

jbtCelF.addActionListener(this);

jbtFahC.addActionListener(this);

jbtClearNum.addActionListener(this);

jbtClearCF.addActionListener(this);

jbtClearBoth.addActionListener(this);

jbtClear.addActionListener(this);

jbtClose.addActionListener(this);

}

publicvoid actionPerformed(ActionEvent e){

String actionCommand = e.getActionCommand();

if (e.getSource()instanceof JButton){

if ("Convert to Fahrenheit".equals(actionCommand))

calculate1();

if ("Convert to Celsius".equals(actionCommand))

calculate2();

else

if ("Clear All".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

jtfResult.setText(" ");

}

if ("Clear Degrees and C/F".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

}

if ("Clear Degrees".equals(actionCommand)){

jtfNum.setText(" ");

}

if ("Clear C/F".equals(actionCommand)){

jtfCF.setText(" ");

}

}

else

if ("Exit Program".equals(actionCommand))

System.exit(0);

}

privatevoid calculate1(){

float celfah = ((5/9)*(num1 - 32));

float num1 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Char.parseChar(jtfCF.getText.trim()));

if (num1 == ())

jtfNum.setText("0");

if (cf == ())

jtfCF.setText("C");

if (cf =="F")

jtfResultCF.setText("F");

jtfResult.setText(" + num 1 + ");

if (cf =="C")

jtfResult.setText(" + celfah + ");

jtfResultCF.setText("F");

}

privatevoid calculate2(){

float fahcel = ((9/5)*(num2) + 32);

float num2 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Char.parseChar(jtfCF.getText.trim()));

if (num1 == ())

jtfNum.setText("0");

if (cf == ())

jtfCF.setText("C");

if (cf == ("C"))

jtfResultCF.setText("C");

jtfResult.setText(" + num2 + ");

if (cf =="F")

jtfResult.setText(" + fahcel + ");

jtfResultCF.setText("C");

}

}

[9719 byte] By [an23dya] at [2007-11-27 11:21:59]
# 1

can't compile your code see comment below

private void calculate1() {

float celfah = ((5/9)*(num1 - 32));

float num1 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Char.parseChar(jtfCF.getText.trim()));

if (num1 == ()) // equal to what

jtfNum.setText("0");

if (cf == ()) // equal to what

jtfCF.setText("C");

if (cf == "F")

jtfResultCF.setText("F");

jtfResult.setText(" + num 1 + ");

if (cf == "C")

jtfResult.setText(" + celfah + ");

jtfResultCF.setText("F");

}

private void calculate2() {

float fahcel = ((9/5)*(num2) + 32);

float num2 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Char.parseChar(jtfCF.getText.trim()));

if (num1 == ()) // equal to what

jtfNum.setText("0");

if (cf == ()) // equal to what

jtfCF.setText("C");

if (cf == ("C"))

jtfResultCF.setText("C");

jtfResult.setText(" + num2 + ");

if (cf == "F")

jtfResult.setText(" + fahcel + ");

jtfResultCF.setText("C");

}

}

Yannixa at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 2

if (cf == ("C"))

Double quotes are for Strings. Single quotes are for chars. Also the brackets around the letter are not needed.

floundera at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 3

flounder is right. it should be

if (cf == 'C') // because cf is char

and also this:

// no such thing as Char.parseChar

// Use Character but Character has also no parseChar.

// you are getting a text, getText return string not char

char cf = (Char.parseChar(jtfCF.getText.trim()));

Yannixa at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks, I think I fixed all the quote things, but what should I do instead of the Char.parseChar? When I try to compile these errors come up.

C:\Documents and Settings\Administrator\My Documents\Temp1.java:93: cannot find symbol

symbol : variable getText

location: class javax.swing.JTextField

char cf = (Char.parseChar(jtfCF.getText.trim()));

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:93: cannot find symbol

symbol : variable Char

location: class Temp1

char cf = (Char.parseChar(jtfCF.getText.trim()));

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:94: incomparable types: float and java.lang.String

if (num1 == (" "))

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:97: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfCF.setText('C');

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:99: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfResultCF.setText('F');

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:101: incomparable types: char and java.lang.String

if (cf == "C")

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:103: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfResultCF.setText('F');

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:109: cannot find symbol

symbol : variable getText

location: class javax.swing.JTextField

char cf = (Character.parseCharacter(jtfCF.getText.trim()));

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:110: incomparable types: float and java.lang.String

if (num1 == (" "))

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:113: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfCF.setText('C');

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:115: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfResultCF.setText('C');

^

C:\Documents and Settings\Administrator\My Documents\Temp1.java:119: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (char)

jtfResultCF.setText('C');

^

an23dya at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 5

Also heres my slightly modified code again if it'll help.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class Temp1 extends JFrame implements ActionListener {

private JTextField jtfNum, jtfCF, jtfResult, jtfResultCF;

private JButton jbtCelF, jbtFahC, jbtClear, jbtClearBoth, jbtClearNum, jbtClearCF, jbtClose;

private float num1, num2, celfah, fahcel;

private char cf;

public static void main(String [] args) {

Temp1 temp = new Temp1();

temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

temp.setSize(800,100);

temp.setTitle("Temperature C/F Converter");

temp.setVisible(true);

}

public Temp1() {

JPanel p1 = new JPanel();

p1.setLayout( new FlowLayout());

p1.add(new JLabel("Degrees : "));

p1.add(jtfNum = new JTextField(10));

p1.add(new JLabel("C/F : "));

p1.add(jtfCF = new JTextField(2));

p1.add(new JLabel("Result : "));

p1.add(jtfResult = new JTextField(10));

jtfResult.setEditable(false);

p1.add(jtfResult = new JTextField(2));

jtfResultCF.setEditable(false);

JPanel p2 = new JPanel();

p2.setLayout(new FlowLayout());

p2.add(jbtCelF = new JButton("Convert to Fahrenheit"));

p2.add(jbtFahC = new JButton("Convert to Celsius"));

p2.add(jbtClose = new JButton("Exit Program"));

p2.add(jbtClearNum = new JButton("Clear Degrees"));

p2.add(jbtClearCF = new JButton("Clear C/F"));

p2.add(jbtClearBoth = new JButton("Clear Degrees and C/F"));

p2.add(jbtClear = new JButton("Clear All"));

getContentPane().setLayout( new BorderLayout());

getContentPane().add(p1, BorderLayout.NORTH);

getContentPane().add(p2, BorderLayout.CENTER);

jbtCelF.addActionListener(this);

jbtFahC.addActionListener(this);

jbtClearNum.addActionListener(this);

jbtClearCF.addActionListener(this);

jbtClearBoth.addActionListener(this);

jbtClear.addActionListener(this);

jbtClose.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

String actionCommand = e.getActionCommand();

if (e.getSource() instanceof JButton) {

if ("Convert to Fahrenheit".equals(actionCommand))

calculate1();

if ("Convert to Celsius".equals(actionCommand))

calculate2();

else

if ("Clear All".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

jtfResult.setText(" ");

}

if ("Clear Degrees and C/F".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

}

if ("Clear Degrees".equals(actionCommand)){

jtfNum.setText(" ");

}

if ("Clear C/F".equals(actionCommand)){

jtfCF.setText(" ");

}

}

else

if ("Exit Program".equals(actionCommand))

System.exit(0);

}

private void calculate1() {

float celfah = ((5/9)*(num1 - 32));

float num1 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Char.parseChar(jtfCF.getText.trim()));

if (num1 == (" "))

jtfNum.setText("0");

if (cf == (' '))

jtfCF.setText('C');

if (cf == 'F')

jtfResultCF.setText('F');

jtfResult.setText(" + num 1 + ");

if (cf == "C")

jtfResult.setText(" + celfah + ");

jtfResultCF.setText('F');

}

private void calculate2() {

float fahcel = ((9/5)*(num2) + 32);

float num2 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (Character.parseCharacter(jtfCF.getText.trim()));

if (num2 == (" "))

jtfNum.setText("0");

if (cf == (' '))

jtfCF.setText('C');

if (cf == 'C')

jtfResultCF.setText('C');

jtfResult.setText(" + num2 + ");

if (cf == 'F')

jtfResult.setText(" + fahcel + ");

jtfResultCF.setText('C');

}

}

an23dya at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 6

Most of these errors are explained by the message.

There is no parseChar method (as Yannix said). Try using the charAt method instead to get a single char.

The setText method has a String parameter but you are passing a char.

num1 is a float and you are trying to compare it to a String. Not allowed.

Once again you should try to work these errors out yourself.

floundera at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...
# 7

here's a hint on using charAt()

char cf;

cf = jtfCF.getText().charAt(index);

if (cf == 'C')

jtfCF.setText("C");

if (cf == 'F'){

jtfResultCF.setText("F");

Message was edited by:

Yannix

Yannixa at 2007-7-29 14:52:13 > top of Java-index,Java Essentials,New To Java...