• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Compilation problem when working with ArrayLists and Collections

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic