File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compilation problem when working with ArrayLists and Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation problem when working with ArrayLists and Collections" Watch "Compilation problem when working with ArrayLists and Collections" New topic
Author

Compilation problem when working with ArrayLists and Collections

java cert
Greenhorn

Joined: Mar 15, 2005
Posts: 1
i typed the following code :

import java.util.*;

class Somethin{

public static void main(String args[])
{

ArrayList ar = new ArrayList();


ar.add(new Integer(2));
ar.add(new Integer(3));
ar.add(new Integer(5));
ar.add(new Integer(7));
ar.add(new Integer(9));




Object m[] = ar.toArray();
int sum=0;

for(int i=0;i<m.length;i++)
sum+=((Integer)m[i]).intValue();
System.out.println("The sum is:"+sum);

}
}

However it is not compiling properly.I have no idea what is happening when i give the Compile command: javac
But i get the following messages (just a few sample lines)

.\Object.java:67: 'class' or 'interface' expected
 <SCRIPT type="text/javascript">
^
.\Object.java:67: 'class' or 'interface' expected
 <SCRIPT type="text/javascript">
^
.\Object.java:70: unclosed character literal
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B>
</A>');
^
.\Object.java:70: unclosed character literal
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B>
</A>');

^
.\Object.java:102: <identifier> expected
<DT>public class <B>Object</B></DL>
^
.\Object.java:105: '{' expected
Class <code>Object</code> is the root of the class hierarchy.
^
.\Object.java:105: '{' expected
Class <code>Object</code> is the root of the class hierarchy.
^
Can NEone please help me out here.I tried solving this for two days now ,but to no avail.

Thanks a lot.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You appear to be trying to compile a web page. Your Somethin class needs to be written in the file Somethin.java, which cannot contain anything except Java code. Then, presuming you pass this file as an argument to javac and not another one, things should work.
[ March 16, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
U have named ur java file as "Object.java" but ur class notation starts like "class somethin"... which is not possible .. as java specification says that a java file name should be same as the name of the class which has the public static void main (obviously if it is present)..also that there can be n number of classes in a .java file but out of these there should be only 1 class having the public static void main.. so in order to solve ur prob do either of the 2 things:

1. Rename ur file as somethin.java (same as ur class name)
2. change ur class name (somethin) to class Object....


Thnx
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Nischal Tanna:
U have named ur java file as "Object.java" but ur class notation starts like "class somethin"... which is not possible


It's not especially good practice, but it's entirely legal. Only public classes need to be in a file named after themselves.


as java specification says that a java file name should be same as the name of the class which has the public static void main (obviously if it is present)


No, the file naming has nothing whatsoever to do with the presence, absence, or location of a "main" routine, only on whether the class itself is public or not.


..also that there can be n number of classes in a .java file but out of these there should be only 1 class having the public static void main.



Nope. You could have 10 classes in a file, and every one could have a "main" routine. Again, filenaming is based on whether the class is public or not.


1. Rename ur file as somethin.java (same as ur class name)
2. change ur class name (somethin) to class Object....


1) is good advice, but 2) is very bad advice; naming classes the same as core classes (like Object) is a bad idea.

Finally, note that nothing in this post relates to the actual problem, which is, as Paul stated, that "Object.java" seems to contain a bunch of HTML, not the Java code shown above.


[Jess in Action][AskingGoodQuestions]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Java cert"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
This is strange. If that is your first program in Java, then it seems you are trying to run before you can walk. Write a "Hello World" program, compile it, run it, review it, change it, run it again and think about why things are they way they are. Only then, once you have a basic understanding should you begin creating more complex classes. You should also buy a book, as almost all introductory texts have a chapter dedicated to the basics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation problem when working with ArrayLists and Collections