Ask coding questions

← Back to all posts
I am VERY confused as to why this dont work
h
JULIODIAZ1 (45)

I just started learning java and to show my so called skillz im gonna try and make a genetic algorithm and if you dont know what that is click here for a video explaining it. So im going to make an algorithm that learns to say the string "Hello World" but i get some errors and i cant fix. someone pls help.

Answered by SixBeeps (5036) [earned 5 cycles]
View Answer
Comments
hotnewtop
SixBeeps (5036)

Unlike in Python, variables are not global by default. Therefore, you need to initialize them in the scope of the class. Put this line in the same hierarchical level as the main and grabChar methods:

ArrayList<String> chars;

and instead of declaring it in main, just assign it:

chars = new ArrayList<String>();
JULIODIAZ1 (45)

well now theres 100 errors @SixBeeps

JULIODIAZ1 (45)

oh wait i think ik how to fix @SixBeeps

SixBeeps (5036)

@JULIODIAZ1 Now all of your array operations are outside of the main method. You can't do anything with the array without being inside of a method. Keep all of your add statements in main, just declare the array outside of it.

InvisibleOne (2674)

correct me if I'm wrong, but it looks like you have never initiated a variable chars.