954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Syntax error, but I just can't figure it out!!! :'(

//This program will prompt for user to enter in the Fahrenheit temperature and convert it to Celsius.

import java.util.Scanner;
public class Project4
{
public static void main (String[]args)
{
double temperature;
double celsius;
Scanner scan = new Scanner (System.in);

System.out.println("Enter the current temperature in Fahrenheit: ");
temperature = scan.nextDouble();

celsius = ((temperature-32)*5/9);
}
System.out.println("So the temperature in Celsius is " + celsius);
}

The final println command has a syntax error with "println". Why is that?

ibthevivin
Newbie Poster
23 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Because it is outside the main function :P

roshan_iiita
Light Poster
30 posts since Nov 2011
Reputation Points: 10
Solved Threads: 1
 

You have to move the

System.out.println("So the temperature in Celsius is " + celsius);


line into the main method. At the moment it is placed outside the main method

Put the closing curly brace in line 14 just after the System.out.println line which gives the error.

niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

Thanks!

I realized that and moved it within the main function and then I was on my way to delete this thread. But hey, I'll leave it up as a thanks and help for others.

ibthevivin
Newbie Poster
23 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

//Have the to floating numbers add together, divided, and multiplied.
public class Project3
{
public static void main (System[]args)
{
float a,b;

a=(float) 1.23;
b=(float) 2.34;

System.out.println("The sum of the two numbers is " a+b);
}
}

I don't know why this one won't compile! It's within the main function. O.o

ibthevivin
Newbie Poster
23 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

In java we use '+' for String concatenation. So in the System.out.println method you are trying to concatenate without using '+'.

Try this

System.out.println("The sum of the two numbers is "+ (a+b));


Also try to understand the difference between above line and the following line

System.out.println("The sum of the two numbers is "+ a+b);
niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

ugh. Amateur mistake. I'll be more careful for little things like that. Thanks again. I'm sure I'll be back. I'm driven to fully understand programming.

ibthevivin
Newbie Poster
23 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Try to understand the compilation error message given by the compiler :) :)

niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: