aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can anyone help me to fix this little problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can anyone help me to fix this little problem" Watch "can anyone help me to fix this little problem" New topic
Author

can anyone help me to fix this little problem

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
public class Main {

/** Creates a new instance of Main */
public Main(int...num) {
int i;
int sum = 0;
for(i=0;i<num.length;i++)
{
sum += num[i];
}
System.out.println(num);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main mt = new Main (12,14,14);

}

}
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
It's a mere typo. In the constructor, after the calculation is finished, you're printing "num" instead of "sum".
Deepak Sagar
Greenhorn

Joined: Dec 18, 2005
Posts: 6
As per statement "Main mt = new Main (12,14,14);" you are passing three int values. But in constructor, argument is considered as array.

In order to send the initialized array, can declare the int array variable and pass that array while creating the instance.



public class Main {

/** Creates a new instance of Main */
public Main(int[] num) {
int i;
int sum = 0;
for(i=0;i<num.length;i++)
{
sum += num[i];
}
System.out.println(sum);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int []arr={12,13,14}; //Array Intialization
Main mt = new Main (arr);

}

}

Please correct me if I am wrong
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
You really can pass an initialized array as a var-arg parameter, that's true. But you also can pass several values, as in vuthlarhi donald 's code, nothing's wrong here. Actually, that's the point of var-arg.
[ December 18, 2007: Message edited by: Serge Petunin ]
 
wood burning stoves
 
subject: can anyone help me to fix this little problem