Solved

Centigrade to Fahrenheit

Posted on 2009-07-08
16
628 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
Setting strict transport security header in header not working 1 78
maven example error 3 52
tomcat not starting 6 24
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now