1,105,625 Community Members

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

Member Avatar
ibthevivin
Junior Poster in Training
50 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
//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?

Member Avatar
roshan_iiita
Light Poster
30 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Because it is outside the main function :P

Member Avatar
niranga
Junior Poster
197 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
ibthevivin
Junior Poster in Training
50 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
ibthevivin
Junior Poster in Training
50 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
niranga
Junior Poster
197 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
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);
Member Avatar
ibthevivin
Junior Poster in Training
50 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
niranga
Junior Poster
197 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: