# Thread: Problems calling methods with arrays.

1. Member
Join Date
Feb 2010
Posts
15
Rep Power
0

## Problems calling methods with arrays.

I decided to write my first genetic algorithm today for fun, but I am getting many ".class expected" errors that seem to have something to do with the way I use arrays. (I've never used arrays like this before, so there's probably something really simple that I'm not understanding about them.)
The goal of the program is to create a 16 bit binary sequence that matches the one given using genetic-style methods.
As I'm new to this, there are probably better ways of doing this, but thats not really the issue.

Java Code:
```import java.util.Random;

public class GeneticTesting{
public static void main(String[] args){
Random rand = new Random();
int goal[] = {1,0,0,0,0,0,0,1,0,1,1,0,0,0,1,0};
int generation[][] = generate();
int closetMatch[] = getClosestMatch(generation[][], goal[]);
int success = checkClosestMatch(closestMatch[] , goal[]);
int iterations = 1;
while(success != true){
generation[][] = mutate(closestMatch[]);
closetMatch[] = getClosestMatch(generation[][], goal[]);
success = checkClosestMatch(closestMatch[], goal[]);
iterations++;
}
System.out.println("Iterations: " + iterations);
}
public int[][] generate(){
for(int x=0;x<10;x++){
for(int y=0;y<16;y++){
generation[x][y] = rand.nextInt(1);
}
}
return generation[][];
}

public int[] getClosestMatch(int generation[][] , int goal[]){
int closestMatch = 0;
int highestMatch = 0;
for(int x=0;x<10;x++){
int matches = 0;
for(int y=0;y<16;y++){
if(equals(goal[y],generation[x][y]){
matches++;
}
if(matches > highestMatch){
matches = highestMatch;
closestMatch = x;
}
}
}
return generation[x][];
}
public boolean checkClosestMatch(int closestMatch[] , int goal[]){
if(deepEquals(closestMatch[], goal[])){
return true;
}
else {
return false;
}
}
public int[][] mutate(int closestMatch[]){
int generation[9][15];
generation[1][] = closestMatch[];
for(x=1;x<10;x++){
generation[x][rand.newRand(7)] = rand.newRand(1);
generation[x][rand.newRand(7)+8] = rand.newRand(1);
}
return generation[][];
}
}```
Could anyone show what I'm doing wrong? Thanks.

2. ## Re: Problems calling methods with arrays.

Post the exact error messages you are getting.

3. Member
Join Date
Feb 2010
Posts
15
Rep Power
0

## Re: Problems calling methods with arrays.

Java Code:
```/Users/Paul/Programs/Java/GeneticTesting.java:8: '.class' expected
int closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:8: '.class' expected
int closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:9: '.class' expected
int success = checkClosestMatch(closestMatch[] , goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:9: '.class' expected
int success = checkClosestMatch(closestMatch[] , goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:12: not a statement
generation[][] = mutate(closestMatch[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:12: ';' expected
generation[][] = mutate(closestMatch[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:12: '.class' expected
generation[][] = mutate(closestMatch[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:13: not a statement
closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:13: ';' expected
closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:13: '.class' expected
closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:13: '.class' expected
closetMatch[] = getClosestMatch(generation[][], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:14: '.class' expected
success = checkClosestMatch(closestMatch[], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:14: '.class' expected
success = checkClosestMatch(closestMatch[], goal[]);
^
/Users/Paul/Programs/Java/GeneticTesting.java:25: '.class' expected
return generation[][];
^
/Users/Paul/Programs/Java/GeneticTesting.java:34: ')' expected
if(equals(goal[y],generation[x][y]){
^
/Users/Paul/Programs/Java/GeneticTesting.java:43: illegal start of expression
return generation[x][];
^
/Users/Paul/Programs/Java/GeneticTesting.java:46: '.class' expected
if(deepEquals(closestMatch[], goal[])){
^
/Users/Paul/Programs/Java/GeneticTesting.java:46: '.class' expected
if(deepEquals(closestMatch[], goal[])){
^
[Finished]/Users/Paul/Programs/Java/GeneticTesting.java:54: ']' expected
int generation[9][15];
^
/Users/Paul/Programs/Java/GeneticTesting.java:54: illegal start of expression
int generation[9][15];
^
/Users/Paul/Programs/Java/GeneticTesting.java:54: ';' expected
int generation[9][15];
^
/Users/Paul/Programs/Java/GeneticTesting.java:54: not a statement
int generation[9][15];
^
/Users/Paul/Programs/Java/GeneticTesting.java:54: ';' expected
int generation[9][15];
^
/Users/Paul/Programs/Java/GeneticTesting.java:55: illegal start of expression
generation[1][] = closestMatch[];
^
/Users/Paul/Programs/Java/GeneticTesting.java:55: '.class' expected
generation[1][] = closestMatch[];
^
/Users/Paul/Programs/Java/GeneticTesting.java:60: '.class' expected
return generation[][];
^
26 errors```

4. ## Re: Problems calling methods with arrays.

You only use the square brackets when accessing an element inside the array. If you want to refer to the entire array then you just use the variable name.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•