Start New Discussion Reply to this Discussion 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?
Related Article: Can't find the logic error...
is a solved Java discussion thread by Tarkenfire that has 4 replies and was last updated 2 years ago.
ibthevivin
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Because it is outside the main function :P
roshan_iiita
Light Poster
30 posts since Nov 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
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
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0
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
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 "[COLOR="Red"] a[/COLOR]+b);
}
}
I don't know why this one won't compile! It's within the main function. O.o
ibthevivin
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0
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
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Try to understand the compilation error message given by the compiler :) :)
niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0774 seconds
using 2.76MB