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);
    }   
}

Recommended Answers

All 11 Replies

i have used array initialisation correctly as: int m[] = {2,3,4};

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)

Error is at line 8 of aray1.
Description of error: '.class' expected int ct = a.getCipherText(m[],l);

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);

thanks..

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);

there occured compilation error as: ArrayIndexOutOBoundsException: 3
at passingArray.java:8
at array1.java:8

please help

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[]

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.

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.

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;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.