Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Plz help, why won't this println?

Status
Not open for further replies.

zee3

Programmer
Feb 26, 2002
4
GB
Ok i'm a newbie at java got everything to compile but it won't do any of the println statements when i execute it, and help would be appreciated


class Hashtable {

public long[] hash;

public static void main (String [] args){
Hashtable h = new Hashtable();
}

public Hashtable Hashtable(){

long[] hash = new long[6000];

hash[0] = 55;
int i = 0;

for (i = 0; i< 6000; i++){
System.out.println(hash[ i ]);}

return Hashtable();

}
}
 
That is because you are using a method same with your class's constructor name.

replace your method with this code below:

public Hashtable(){

long[] hash = new long[6000];

hash[0] = 55;
int i = 0;

for (i = 0; i< 6000; i++){
System.out.println(hash[ i ]);}

return ;
}

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Your setting your variable hash as an array of long's with a size of 6000. But you are only setting the first item to have a value (55).

Your println call is working but you are printing out 6000 lines of which the last 5999 are blank.
 
Makes no sense to me.
As first, the compiler creates a new instance of the class and the method is not visited.
Secondly if it were it should have printed the first value as &quot;55&quot; and the others as &quot;0&quot; because primitive variables like int,long,double,float,boolean have initial values. boolean --> false,others -- 0 IF they are defined in an ARRAY.

If you make a code like that

{
...
int a;
...
System.out.println(a);
...
}
It won't compile and will give the error local variable a may not have been initialized...


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top