methods... SO confused!

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-08-2011, 03:28 AM
ertopp
methods... SO confused!
I have to write a methods program to calculate celsius and i am so lost. can anyone please help me? I keep getting "cannot find symbol" errors.

public class FahrenheitToCelsius {

public static void main(String[] args)
{
float fahrenheit, celsius;
String input;
celsius = calculateCelsius(fahrenheit);

input = JOptionPane.showInputDialog("Enter the Fahrenheit temperature you wish to convert as a floating point number.");
//Convert the input to float.
fahrenheit = Float.parseFloat(input);
}

//Calculate Celsius
public static float calculateCelsius(float f)
{
float celsius, fahrenheit;

celsius = calculateCelsius(fahrenheit);
return(5/9) * (fahrenheit-32);
• 10-08-2011, 03:46 AM
jammas615
Re: methods... SO confused!
If you look at your code, your giving celsius a value of calculateCelsius(fahrenheit). You need to look at the order you have things occuring and rearrange some stuff.
• 10-08-2011, 03:48 AM
ertopp
Re: methods... SO confused!
I'll be honest, I have no idea what you mean. Should that go under the calculatecelsius method? I am so new to Java and basically ready to pull my hair out.
• 10-08-2011, 03:50 AM
ertopp
Re: methods... SO confused!
I am getting so many errors it's not even funny. I don't have a clue how to change this.
Can anyone tell me why my input boxes aren't even coming up?
• 10-08-2011, 04:07 AM
jammas615
Re: methods... SO confused!
You need to get your input BEFORE you call the calculateCelcius method.
Here:
Code:

```//get input input = JOptionPane.showInputDialog("Enter the Fahrenheit temperature you wish to convert as a floating point number."); //Convert the input to float. fahrenheit = Float.parseFloat(input); //call calculateCelcius celsius = calculateCelsius(fahrenheit);```
• 10-08-2011, 04:09 AM
jammas615
Re: methods... SO confused!
I think you need to do some reading: The Really Big Index
• 10-08-2011, 04:12 AM
ertopp
Re: methods... SO confused!
I actually have read that. I have been all over the net, read my chapter 4 times, watched the book videos and emailed my teacher. I just don't seem to get it. And all of the errors I get aren't seeming to help.
• 10-08-2011, 04:19 AM
jammas615
Re: methods... SO confused!
The best thing to do is write what you want to do down:
1. Ask user to input temperature in Fahrenheit.
2. convert input to Celsius
3. Show user temperature in Celsius.

and now go through what you need to do this:
1. A float variable for Fahrenheit
2. A way to put user input into Fahrenheit variable
3. A method that returns a float that converts Fahrenheit to Celsius
4. Print the result
3.
• 10-08-2011, 04:25 AM
Fubarable
Re: methods... SO confused!
Quote:

Originally Posted by ertopp
I actually have read that. I have been all over the net, read my chapter 4 times, watched the book videos and emailed my teacher. I just don't seem to get it. And all of the errors I get aren't seeming to help.

• 10-08-2011, 04:29 AM
ertopp
Re: methods... SO confused!
I did make a separate post with the errors, thank you. and I am not whining. Just saying that I have read all of that material and it wasn't helpful. Thats why I came here. I was hoping someone could help me. I am not asking for the answer, I just want to understand better. I am not terrible with Java. Methods confuse me and I want to learn why things are the way they are.
• 10-08-2011, 04:31 AM
ertopp
Re: methods... SO confused!
I tweeked my program a bit and will copy what I have now. Does this not seem to match the algorithm you just posted? I am only asking because I feel like it makes sense to me, then I get a ton of errors on the public static float calculateCelsius line.

This is what I have:

Code:

```public class FahrenheitToCelsius {   public static void main(String[] args){   {           float fahrenheit, celsius;           String input;           input = JOptionPane.showInputDialog("Enter the Fahrenheit temperature you wish to convert as a floating point number.");           //Convert the input to float.           fahrenheit = Float.parseFloat(input);           celsius = calculateCelsius(fahrenheit);           celsius = JOptionPane.showMessageDialog(null, fahrenheit + " degrees fahrenheit =" + celsius + " degrees celsius");   }           //Calculate Celsius           public static float calculateCelsius(float fahrenheit)           {                   celsius = (5/9) * (fahrenheit - 32);                   return celsius;           }                 } }```
• 10-08-2011, 04:33 AM
Fubarable
Re: methods... SO confused!
Quote:

Originally Posted by ertopp
I did make a separate post with the errors, thank you. and I am not whining. Just saying that I have read all of that material and it wasn't helpful. Thats why I came here. I was hoping someone could help me. I am not asking for the answer, I just want to understand better. I am not terrible with Java. Methods confuse me and I want to learn why things are the way they are.

Your problem is that you need to study more as that's the only way to clear the confusion. Serious, it will help. Now for your current problem, please post your error messages here to keep the discussion from being fragmented. You should show which line is causing which error and show the actual error message itself.

You will also want to change your style of programming. If you're finding your code is showing lots of errors, it means that you're adding code to bad code, something you should never do. Instead start with a simple program skeleton, be sure it compiles and then every time you add a new line or two of code, check that it compiles correctly before adding any more code. If it doesn't fix it first.
• 10-08-2011, 04:34 AM
ertopp
Re: methods... SO confused!
These are the errors:

line 28: ';' expected
public static float calculateCelsius(float fahrenheit)

line 28: '.class' expected
public static float calculateCelsius(float fahrenheit)

line 28: illegal start of expression
public static float calculateCelsius(float fahrenheit)

line 28: ';' expected
public static float calculateCelsius(float fahrenheit)

line 28: illegal start of expression
public static float calculateCelsius(float fahrenheit)

^
• 10-08-2011, 04:35 AM
ertopp
Re: methods... SO confused!
Ok I do study and am actually doing fairly well. I said methods confuse me. I take online courses so I basically teach myself. It's hard to teach yourself something you don't understand.
• 10-08-2011, 04:40 AM
Fubarable
Re: methods... SO confused!
Check your curly braces. You've got some where you shouldn't. Your code formatting is bad, and this is preventing you from seeing your errors. Use one consistent formatting style, myself, I recommend the one true brace style, but you should use whichever your teachers recommend since they are the ones who absolutely must be able to easily understand your code.

So my assignment to you is this: go through your code, correct its formatting and check each and every brace. Get rid of extra unnecessary braces, and make sure that opening and closing braces match where they are supposed to match.
• 10-08-2011, 04:42 AM
ertopp
Re: methods... SO confused!
But I thought you were to have braces around the code in each method?
• 10-08-2011, 04:42 AM
Fubarable
Re: methods... SO confused!
Quote:

Originally Posted by ertopp
But I thought you were to have braces around the code in each method?

Again, you have an extra set of braces. Find them.
• 10-08-2011, 04:44 AM
ertopp
Re: methods... SO confused!
Also, when I change braces around I get this error:
line 40: reached end of file while parsing

I use the layout provided by my book so I didn't know it was that bad.
• 10-08-2011, 04:45 AM
Fubarable
Re: methods... SO confused!
Look carefully here:

Code:

```  public static void main(String[] args){   {     float fahrenheit, celsius;     String input;```
See anything extra?
• 10-08-2011, 04:51 AM
ertopp
Re: methods... SO confused!
yes, i do now. sorry. I have been staring at this program for hours.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last