Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation problem when working with ArrayLists and Collections

 
java cert
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic