Closed Thread Icon

Preserved Topic: Java help Pages that link to <a href="https://ozoneasylum.com/backlink?for=20931" title="Pages that link to Preserved Topic: Java help" rel="nofollow" >Preserved Topic: Java help\

 
Author Thread
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-29-2001 09:57

Could someone please tell me:

  1. Is there something wrong with my boolean statements i.e. if (degreeType == 'F') or (loop != 'Q')
  2. Is there an easier way to write this program



I get the feeling I made this exercise harder than it had to be. Any ideas for simplifying my code would be appreciated. I also don't understand why my variables are not showing up in all the places they should. I know I'm getting the correct input into my variables, because I cut the top half of the code off and ran it by itself. All the variables were holding the values I expected. The values just don't seem to make it through the whole program. Here's the offending code:

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>public class TempConverter
{
public static void main(String[] args)
{
double degreesF, degreesC, degrees;
String answer, degreesStr;
char degreeType, loop;
int answerLength;

do
{

System.out.println("Enter a temperature like 98.6F.");
System.out.println("Be sure to end the temperature with either");
System.out.println("a C for celsius, or an F for fahrenheit.");
answer = SavitchIn.readLine();
answerLength = answer.length();
degreeType = answer.charAt(answerLength-1);
degreesStr = answer.substring(0,answerLength-1);
degrees = Double.parseDouble(degreesStr);

if ((degreeType != 'F')

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-29-2001 23:43

Your logic is wrong as well as your math.

Nothing major, have to go eat this second will have fixed code when I get back.

-mage-

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-30-2001 00:49

[code]
public class TempConverter
{
public static void main(String[] args)
{
double degreesF, degreesC, degrees;
degreesF = 0;
String answer, degreesStr;
char degreeType, loop;

do
{
System.out.println("Enter a temperature like 98.6F.");
System.out.println("Be sure to end the temperature with either");
System.out.println("a C for celsius, or an F for fahrenheit.");
answer = SavitchIn.readLine();
degreeType = answer.charAt(answer.length()-1);
degreesStr = answer.substring(0,answer.length()-1);
degrees = Double.parseDouble(degreesStr);
if (degreeType == 'C' &#0124; &#0124; degreeType == 'c')
{
degreesF = ((9/5) * degrees) + 32;
System.out.println("This temperature, " + answer);
System.out.println("converts to " + degreesF + "F.");
}
else if (degreeType == 'F' &#0124; &#0124; degreeType == 'f')
{
degreesC = (5/9)*degrees - (5/9)*32;
System.out.println("This temperature, " + answer);
System.out.println("converts to " + degreesC + "C.");
}
else
{
System.out.println("You have not entered a proper temperature type.");
System.out.println("Please enter C for celsius or F for fahrenheit.");
degreeType = SavitchIn.readLineNonwhiteChar();
}

System.out.println("Enter Q to quit the program.");
System.out.println("Press any other key to convert another temperature.");
loop = SavitchIn.readLineNonwhiteChar();
}
while (loop != 'Q');
}
}
[code]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-30-2001 01:45

Thanks Mage

I still need to learn to remember the cascading effect of if { else if { else statements. I kind'a keep repeating myself with mine.

There's one problem though. In the code you wrote, if the user forgets to enter a temperature type, after they are asked for it again, it does't loop back up to complete the calculations. Also, The conversion to celsius always returns a 0.0 value

Thanks

[This message has been edited by butcher (edited 04-30-2001).]

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-30-2001 04:23

I made the same math error I said you did.

code:
public class TempConverter
{
public static void main(String[] args)
{
double degreesF, degreesC, degrees;
degreesF = 0;
String answer, degreesStr;
char degreeType, loop;
do
{
System.out.println("Enter a temperature like 98.6F.");
System.out.println("Be sure to end the temperature with either");
System.out.println("a C for celsius, or an F for fahrenheit.");
answer = SavitchIn.readLine();
degreeType = answer.charAt(answer.length()-1);
degreesStr = answer.substring(0,answer.length()-1);
degrees = Double.parseDouble(degreesStr);
if (degreeType == 'C' &#0124; &#0124; degreeType == 'c')
{
degreesF = ((9/5) * degrees) + 32;
System.out.println("This temperature, " + answer);
System.out.println("converts to " + degreesF + "F.");
}
else if (degreeType == 'F' &#0124; &#0124; degreeType == 'f')
{
degreesC = ((5*degrees)/9) - (160/9);
System.out.println("This temperature, " + answer);
System.out.println("converts to " + degreesC + "C.");
}
else
{
System.out.println("You have not entered a proper temperature type.");
System.out.println("Please enter C for celsius or F for fahrenheit.");
degreeType = SavitchIn.readLineNonwhiteChar();
}

System.out.println("Enter Q to quit the program.");
System.out.println("Press any other key to convert another temperature.");
loop = SavitchIn.readLineNonwhiteChar();
}
while (loop != 'Q');
}
}

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-30-2001 04:26

The if-else logic thing was a bit complex. You could do it the way you had proposed, but you have to do it in a different format. I really didn't feel like figuring that out when there was a much easier way to do it.

if ((degreeType != 'F')

« BackwardsOnwards »

Show Forum Drop Down Menu