Results 1 to 4 of 4
  1. #1
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Question 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Problems calling methods with arrays.

    Post the exact error messages you are getting.

  3. #3
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default 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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default 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.

Similar Threads

  1. calling methods in other classes
    By elite 98 in forum New To Java
    Replies: 5
    Last Post: 07-09-2011, 03:33 AM
  2. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  3. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  4. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:44 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Tags for this Thread

Posting Permissions

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