Passing array with its length to a method
please check the following piece of code and give the correct code as my program is showing some error. here is the code.
public class passingArray
{
public int getCipherText(int[] c, int len)
{
int cipher = 0;
int e = 7;
int n = 130;
cipher = (int)((Math.pow(c[len],e)) % n);
return cipher;
}
}
public class array1
{
public static void main(String[] ar)
{
int m[] = {2.4,5};
int l = m.length;
passingArray a = new passingArray();
int ct = a.getCipherText(m[], l);
System.out.print(ct);
}
}
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
i have used array initialisation correctly as: int m[] = {2,3,4};
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
1. Post your code in code code tags so we can read it easily
2. If you have an error tell us what it is! - Exact complete error message(s) including the line number(s)
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Error is at line 8 of aray1.
Description of error: '.class' expected int ct = a.getCipherText(m[],l);
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
m is the name of the array you declared, so the [] in m[] on that line is invalid syntax. To pass the array named m you just need int ct = a.getCipherText(m,l);
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
OK
ps You don't need to pass the length of the array with the array - you can always query the length of any array whenever you need it with .length, as in
cipher = (int)((Math.pow(c[c.length],e)) % n);
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
there occured compilation error as: ArrayIndexOutOBoundsException: 3
at passingArray.java:8
at array1.java:8
please help
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
there occured compilation error as: ArrayIndexOutOBoundsException: 3
at passingArray.java:8
at array1.java:8
please help
because java starts at 0, so if your array length is 3(ie has 3 variables), to get the last index you will use array.length-1.
but also see here:
int m[] = {2.4,5};//how can 2.4 be an integer? so your array size is 2, but it will also round down the value 2.4->2
if you are using decimals, rather create a double[]
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
there occured compilation error...
Sorry - I was illustrating how you didn't need to pass the length, but I didn't point out the logic error that was there in the first place.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
in my actual program, i have used int m[] = {2,4,5}; and not int m[]={2.4,5};
it happened here by mistake while as my actual program used int m[] ={2,4,5}; but it also showed the same error. please help for error, because syntax is all ok.
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
in my actual program, i have used int m[] = {2,4,5}; and not int m[]={2.4,5};
it happened here by mistake while as my actual program used int m[] ={2,4,5}; but it also showed the same error. please help for error, because syntax is all ok.
did you read my post fully... you cant do this:
cipher = (int)((Math.pow(c[len],e)) % n);
the problem is you are trying to access index 3, however java starts at 0... hence 0,1,2 therefore 2 holds the last index try rather:
cipher = (int)((Math.pow(c[len-1],e)) % n);
[edit] also depending on the output you want you may want to use a for statement to loop through the values in the array, and check your return value cipher, even if you use a for statement it will only return the answer of the last array index this case:125, maybe you wnat to loop through each variable, then create a new array with the answers and return that instead of a simple int
it would then look something similar too:
public int[] getCipherText(int[] c) {
...
for (int i = 0; i < c.length; i++) {
cipher = (int) ((Math.pow(c[i], e)) % n);
c[i] = cipher;
}
return c;
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169