Tuesday, July 28, 2009

Outputting a result in the same box as the input?

I'm having difficulties with my celsius/fahrenheit converter in Java.. I need to output the result in either the celsius box or the fahrenheit box, whichever one they didn't put a number in...





the code is pretty long.. and it wont let me paste the whole code.. basically i'm using JFrames, a GUI and here's the part i think i'm having difficulty with...





private void calculate( char operator ) {


double Cel = (Double.parseDouble(jtfCel.getText().tri...


double Far = (Double.parseDouble(jtfFar.getText().tri...





double Celsius = 0;


double Fahrenheit = 0;





switch ( operator ) {


case 'C': Celsius = ((Far - 32)*(5/9));


break;


case 'F': Fahrenheit = ((9/5)*Cel+32);


break;


case 'L': Cel = 0; Far = 0;


break;


}





//set result in the textfields...


jtfFar.setText(String.valueOf(Fahrenheit...


jtfCel.setText(String.valueOf(Celsius));


}





OH also, how do u clear a textfield in Java?

Outputting a result in the same box as the input?
//set result in the textfields...


jtfFar.setText(String.valueOf(Fahrenhe...


jtfCel.setText(String.valueOf(Celsius)...





Only execute one of them, based on operator. The other one has the number the user entered, so you don't have to write it.





To clear a text field, set it to '' (two single quotes, no space between them).
Reply:Set two string variables to ""


Set the value of each for each text box


check if the variables are not equal to ""


If they are equal, then populate the other box.


Set the box that is not equal to a string value of that is ""


No comments:

Post a Comment