Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Centigrade to Fahrenheit

Posted on 2009-07-08
16
Medium Priority
?
667 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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