Results 1 to 4 of 4
- 11-02-2011, 12:35 AM #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[][]; } }
- 11-02-2011, 12:46 AM #2
Re: Problems calling methods with arrays.
Post the exact error messages you are getting.
- 11-02-2011, 12:50 AM #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
- 11-02-2011, 01:06 AM #4
Similar Threads
-
calling methods in other classes
By elite 98 in forum New To JavaReplies: 5Last Post: 07-09-2011, 03:33 AM -
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02-27-2010, 09:12 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
Problems with readLine() and calling methods
By peachyco in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:44 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM
Bookmarks