The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Question about methods, temperature conversion program

Hey everyone i want to start by saying i am very new to java so i had an issue i wanted to see if anyone can give me a solution. i basically have to make a program that displays a table that shows temperatures from celsius to fahrenheit from 40 degrees celsius down to 31 degrees celsius and convert them to fahrenheit on one side of the table, and on the other side of the table have 120 fahrenheit down to 30 degrees fahrenheit and convert them to celsius.

here is my code so far (i have not yet done the print line to display any of this in a table yet)

Re: Question about methods, temperature conversion program

Suggestions:

Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.

Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.

Plan out the logic of your program before committing it to code as this will save you most of the above grief.

Re: Question about methods, temperature conversion program

Originally Posted by curmudgeon

Suggestions:

Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.

Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.

Plan out the logic of your program before committing it to code as this will save you most of the above grief.

Re: Question about methods, temperature conversion program

Originally Posted by aivory616

thanks for the tips, check back later and i will update you

Good. More specific tips, this line of code:

for(double celsius=40; celsius<=31.0; celsius--){

states that you are creating a double variable named celsius, you are assigning it a value of 40, you state that the loop will continue to re-loop as long as celsius is less than or equal to 31 (which is impossible at the start of the loop since it starts at 40 -- maybe you want to change this condition), and you state that with each iteration of the loop, you will subtract 1 from celsius (that looks OK).

I suggest changing that condition that makes the loop not work. I also suggest that you make celsius an int, since for loops work better with ints in general. You can always convert it to double by casting when you want it treated as a double.

You are saying, take a variable that doesn't exist and pass it into a method, and then print out the results of that method. Perhaps you don't want to use fahrenheit since the method name suggests that it converts a celsius value to a fahrenheit value. What temp would make more sense to pass into this method? celsius or fahrenheit? You already have a variable that you're looping with -- what does it hold? Can you use it instead?

Re: Question about methods, temperature conversion program

The code you've posted won't compile. When you fix it so that it does compile, then you'll find that it does in fact print something. Your job is to fix it so that what it prints out makes sense. Are you allowed to use arrays?

Re: Question about methods, temperature conversion program

yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish. i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.

Re: Question about methods, temperature conversion program

yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish.

It's printing the values obtained from your method but without formatting

i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.

We can't do this for you, and your deadline is your issue not ours, but we can help you. I suggest,

Consider creating an 2-dimensional array of double[10][4], meaning an array with 10 rows and 4 columns which should correspond to your desired output.

Your for loops are currently nested one inside of the other. Don't do this as it doesn't make logical sense. I would use one for loop to calculate the first two columns of the double array, and then a second for loop to calculate the 2nd two columns of the double array.

Re: Question about methods, temperature conversion program

Then you will want to use a single for loop that does for (int i = 0; i < 10; i++) and calculate the four numeric values inside of the loop, and prints out the row of data. You will need to reduce your celsius1 and your fahrenheit1 variables inside the loop and use this to calculate your fahrenheit2 and celsius2 results, then print out celsius1 fahrenheit2 fahrenheit1 celsius1 on each line, with proper formatting of course.