Solved

Java: Error calling System.out.println()

Posted on 2009-05-04
3
497 Views
Last Modified: 2013-11-23
Please tell me why the program doesn't compile and how to fix the problem.
I am trying to compile a java program without using an IDE.  I have set the PATH and CLASSPATH environment variables to point respectively to the bin and lib directories of the jdk.  I am operating under Windows and using the command prompt to compile.  The source  is e:\scjp\exercises\chapter1\ListFoods.java and the working directroy is e:\scjp\exercises\chapter1.  The command I use to compile is javac ListFoods.java. When I compile the code I receive the error message "ListFoods.java:14: <identifier> expected System.out.println(s);
class main{

	String s = "Hi mom";

	System.out.println(s);

}

Open in new window

0
Comment
Question by:ssackett
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24301699
your code needs to go in a method, and your class name needs to be same as file name

class ListFoods {
     public static void main(String[] args) {
        String s = "Hi mom";
        System.out.println(s);
     }
}

0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 24301721
you'll also need to make your class public

public class ListFoods {
     public static void main(String[] args) {
        String s = "Hi mom";
        System.out.println(s);
     }
}

0
 

Author Closing Comment

by:ssackett
ID: 31577889
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now