1

I'm trying to create a temperature converter where the user enters a temperature in Fahrenheit and it returns it in Celsius. Here's the code:

import javax.swing.*;

public class tempconv {

    public static void main (String[]args) {
        int fahr, cel;
        String fahrstring;

        fahrstring = JOptionPane.showInputDialog(valueof("Enter your temperature in F:"));
        fahr = new int[fahrstring];
        cel = (fahr - 32) * 5/9;

        JOptionPane.showMessageDialog(null, "The temperature in c is " + cel);
    }

}

I'm trying to convert the input dialog given into a int but the compiler stops me saying:

error: cannot find symbol
 fahrstring = JOptionPane.showInputDialog(>valueof("Enter your temperature in F:"));

so it must be a syntax error. According to the compiler I also got another error saying

tempconv.java:10: error: incompatible types: int[] cannot be converted to int
                  fahr = >new int[fahrstring];

how would the correct code be written and what exactly am I doing wrong?

4
  • What do you think that valueof("Enter your temperature in F:") should be returning? Why do you think that new int[fahrstring] does? Commented Feb 18, 2016 at 2:13
  • should be a string I just realized I have no conversion the code is flawed the base of it was from my teacher Commented Feb 18, 2016 at 2:15
  • If it's from your teacher they probably wanted you to debug it. Don't use StackOverflow to do your homework for you, mate, it's in the rules ^^; Commented Feb 18, 2016 at 2:22
  • I'll remember that in the future but I've been at this for quite a while so had to ask Commented Feb 18, 2016 at 2:24

3 Answers 3

1

use double rather than int as temperature may be 29.5 sometimes

double fahr = Double.parseDouble(fahrstring);
double c = ((fahr - 32) * (5/9));

Hope your problem is solved . keep coding good luck.

Sign up to request clarification or add additional context in comments.

Comments

0

About the incompatible types error:

int fahr is an integer and new int [fahrString] will create an array object. fahr=new int [fahrstring] means you are trying to assign an array object to an int variable. This is obviously not going to work.

What can you do about it?

  • Change int fahr to int fahr[] and the compatibility error wouldn't show up again.

1 Comment

Give me an example. It depends on your requirement.
0

Another way to convert fahrstring to an int is by using the function:

Integer.parseInt(fahrstring)

(Here is a tutorial which demonstrates parseInt) http://www.tutorialspoint.com/java/lang/integer_parseint.htm

5 Comments

You might want to double-check; I think you're thinking of valueOf. ;)
I'm sure that valueOf ()works but so should parseInt() I edited my answer to show a tutorial for it. (I assume Tutorials point is a reputable enough site)
Sorry, that probably wasn't clear. My comment was in reference to your statement that parseInt returns an Integer.
(I'll double check the documentation)
(Fixed that on my answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.