Solved

Centigrade to Fahrenheit

Posted on 2009-07-08
16
632 Views
Last Modified: 2013-11-24
Im trying to write a program that displays a table of the centigrade tempertaures 0 through 20 and thier Fahrenheit equivilents. the formula for Centigrade to Fahrenheit is C = 9/5 + 32

This is what i have so far.
import javax.swing.JOptionPane;
 
 
public class CentigradeToFahrenheitRevised
{
public static void main(String[] args)
{
	String FahrenheitString;
	double Fahrenheit;
	double Centigrade;
	double tempF;	
	double tempC;	
	
 
	FahrenheitString = JOptionPane.showInputDialog(null, "Enter the Temperature in Fahrenheit " +
											JOptionPane.INFORMATION_MESSAGE);
 
	tempF = Double.parseDouble(FahrenheitString);
	Centigrade = (float)5/9 *(tempF - 32);
 
	JOptionPane.showMessageDialog (null, "The temperature in CENTIGRADES is "+ Centigrade);
	
	System.exit(0);
 
	}
}

Open in new window

0
Comment
Question by:AgentC4
  • 9
  • 7
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24810282
you just have some duplication, try something more like this

import javax.swing.JOptionPane;
 
 
public class CentigradeToFahrenheit
{
public static void main(String[] args)
{
        String FahrenheitString;
        double Fahrenheit;
        double Centigrade;
 
        FahrenheitString = JOptionPane.showInputDialog(null, "Enter the Temperature in Fahrenheit " + "Temperature in FAHRENHEIT", JOptionPane.INFORMATION_MESSAGE);
 
        Fahrenheit = Double.parseDouble(FahrenheitString);
        Centigrade = (float)5.0/9.0 *(Fahrenheit - 32.0);
        JOptionPane.showMessageDialog (null, "The temperature in °C is " + Centigrade);
 
        }
}
0
 

Author Comment

by:AgentC4
ID: 24810298
how would i go about adding a table to show the C to F equivalency. I'm having a hard time starting that
0
 
LVL 92

Expert Comment

by:objects
ID: 24810347
uuse a loop and do the conversion inside the loop

for (int f = 0; f<=20; f++) {
   // do conversion here
   // and output result
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:AgentC4
ID: 24810390
im not the greatest when it comes to the looping but this is what i have.


for (int f = 0; f<=20; f++)
              {
             
         System.out.printf(Centigrade + "Centigrade %0d is %2s Fahrenheit\n", f + 1, Fahrenheit);

         Centigrade = (float)5.0/9.0 *(Fahrenheit - 32.0);
             
              }      
0
 

Author Comment

by:AgentC4
ID: 24810395
everytime i compile and run this is what i get after i enter a tempferature and after it converts


at java.util.Formatter$FormatSpecifier.checkNumeric(Formatter.java:2924)
      at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2884)
      at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2640)
      at java.util.Formatter.parse(Formatter.java:2477)
      at java.util.Formatter.format(Formatter.java:2411)
      at java.io.PrintStream.format(PrintStream.java:899)
      at java.io.PrintStream.printf(PrintStream.java:800)
      at CentigradeToFahrenheitTEST.main(CentigradeToFahrenheitTEST.java:34)
0
 
LVL 92

Expert Comment

by:objects
ID: 24810400
f is the fahrenheight value to compare so use it instead of Fahrenheit var
And calculate celcius before you print it

         Centigrade = (float)5.0/9.0 *(f - 32.0);
         System.out.printf("Centigrade %0d is %2s Fahrenheit\n", Centigrade, f);
0
 

Author Comment

by:AgentC4
ID: 24810424
it runs, but i still get this:

at java.util.Formatter$FormatSpecifier.checkNumeric(Formatter.java:2924)
      at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2884)
      at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2640)
      at java.util.Formatter.parse(Formatter.java:2477)
      at java.util.Formatter.format(Formatter.java:2411)
      at java.io.PrintStream.format(PrintStream.java:899)
      at java.io.PrintStream.printf(PrintStream.java:800)
      at CentigradeToFahrenheitTEST.main(CentigradeToFahrenheitTEST.java:35)
0
 
LVL 92

Expert Comment

by:objects
ID: 24810436
you are using a string format for your farenheight value, try this:

         System.out.printf("Centigrade %0d is %0d Fahrenheit\n", Centigrade, f);
0
 

Author Comment

by:AgentC4
ID: 24810449
same error which is very weird.


at java.util.Formatter$FormatSpecifier.checkNumeric(Formatter.java:2924)
      at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2884)
      at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2640)
      at java.util.Formatter.parse(Formatter.java:2477)
      at java.util.Formatter.format(Formatter.java:2411)
      at java.io.PrintStream.format(PrintStream.java:899)
      at java.io.PrintStream.printf(PrintStream.java:800)
      at CentigradeToFahrenheitTEST.main(CentigradeToFahrenheitTEST.java:35)
0
 
LVL 92

Expert Comment

by:objects
ID: 24810529
sorry not thinking today :) %0d is for integers, your want %f
0
 

Author Comment

by:AgentC4
ID: 24810547
This is what i have and i changed %0d to %0f

for (int f = 0; f<=20; f++)
              {
             
                    Centigrade = (float)5.0/9.0 *(f - 32.0);
                 System.out.printf("Centigrade %0f is %0f Fahrenheit\n", Centigrade, f);
             
              }
0
 

Author Comment

by:AgentC4
ID: 24810551
and it still wont show the table or anything instead i get the same long error message
0
 
LVL 92

Expert Comment

by:objects
ID: 24810606
f is an int, so it needs %d

   System.out.printf("Centigrade %4.2f is %d Fahrenheit\n", Centigrade, f);
0
 

Author Comment

by:AgentC4
ID: 24810668
Yes, that worked thanks again man. Any recommendations as to i should study JAVA so i can get really good at it? any specia tips or tricks?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24810694

The Sun tutorial is a good start http://java.sun.com/docs/books/tutorial/
I also offer an excellent tutoring service :)
0
 

Author Comment

by:AgentC4
ID: 24810723
thanks alot ill keep that in mind.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question