Solved

Centigrade to Fahrenheit

Posted on 2009-07-08
16
626 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 62
mapShare challenge 13 69
Eclipse Neon and jdk 1.8.0 11 72
what is a "java.lang.System Property"   ? 20 41
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 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

13 Experts available now in Live!

Get 1:1 Help Now