?
Solved

Centigrade to Fahrenheit

Posted on 2009-07-08
16
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 

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 2000 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 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