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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters in Arrays

Status
Not open for further replies.

mwn1218

MIS
Jan 17, 2005
11
US
Hey, I'm a newbie to java and am getting a syntax error on my passing of the array in the main method. It does not like that I am passing Num3, Num4. Since its an array do I have to pass it a certain way? Any insight would be helpful, Thanks in advance!


Here is the coding I have in the method:

public static int intArray(int [][] myArray){
int[][] intArray = new int[2][10];
for (int r = 0; r<myArray.length; r++){
for (int c = 0; c<myArray[r].length; c++){
myArray[r][c] = r++;
}//close 2nd for
}//close 1st for
}//close myArray method

Here is the coding I have in the main:

else if (commandtoken.equalsIgnoreCase("NUM")){
int Num3 = Integer.parseInt(st.nextToken());
int Num4 = Integer.parseInt(st.nextToken());
int[][] myArray = new int[Num3][Num4];
System.out.println("Number Array with " + Num3 + " rows and " + Num4 + " columns " + intArray(Num3, Num4));

This is the error I am receiving:

Assign1.java [49:1] intArray(int[][]) in Assign1 cannot be applied to (int,int)

Num4 + " columns " + intArray(Num3, Num4));
^
1 error
Errors compiling main.


 
Code:
System.out.println ("Number Array with " + Num3 + " rows and " + Num4 + " columns " + intArray (myArray));
note:
Code:
public static int intArray(int [][] myArray){
       int[][] intArray = new int[2][10];
the second line defines a new two-dim array, which is never used. Delete it.

seeking a job as java-programmer in Berlin:
 
You want to do :-
Code:
System.out.println("Number Array with " + Num3 + " rows and " + Num4 + " columns " + intArray(myArray));

ie. actually pass the array as the parameter to your method.

Tim
 
Maybe this has nothing to do with this, but that r++ statement will make the r index overflow.

Cheers,

Dian
 
Your right there. He probably meant to do myArray[r][c] = r + 1;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top