EDIT
I have the input 5 3, where 10 is the amoung of characters per line and 5 (StartA) is the position of a * in that line.
I need to save this input in a boolean array so that it saves { false, false, true, false, false } Currently the program gives a nullPointerExeption when executing: CurrentState[i] == true
Can you, please, explain how I program this.
class Program{
Scanner sc = new Scanner(System.in);
int L; // #characters per line
int G; // # lines
int Start; // Starting position
int i; // position of character calculated
int k; // Position in new line
int j; // Row working
String choice; // choice which program to execute
boolean firstLine[] = new boolean[L+1];
boolean nextGen[] = new boolean[L+1];
void input() {
L = sc.nextInt(); // # Characters per line
G = sc.nextInt(); // # lines
Start = sc.nextInt(); // Characters which is a * in first line
}
// output for first line
void line() {
for (i=0; i<L+1; i++) {
if (i == Start-1) {
CurrentState[i] = true;
System.out.print("*");
} else if (i==L) {
System.out.println("");
} else {
CurrentState[i] = false;
System.out.print(".");
}
}
}
// rules for calculating the next line
void rules() {
for ( k=0; k<L-1; k++) {
for ( i=1; i<L-1; i++) {
if (CurrentState[i-1] == true && CurrentState[i]== true && CurrentState[i+1] == true) {
nextGen[k] = false;
} else if (CurrentState[i-1] == true && CurrentState[i] == true && CurrentState[i+1] == false) {
nextGen[k] = true;
} else if (CurrentState[i-1] == false && CurrentState[i] == true && CurrentState[i+1] == true) {
nextGen[k] = true;
} else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == false) {
nextGen[k] = false;
} else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == true) {
nextGen[k] = true;
} else if (CurrentState[i-1] == false && CurrentState[i] == false && CurrentState[i+1] == true) {
nextGen[k] = true;
} else if (CurrentState[i-1] == true && CurrentState[i] == false && CurrentState[i+1] == false) {
nextGen[k] = true;
} else if (CurrentState[i-1]== false && CurrentState[i] == true && CurrentState[i+1]== false) {
nextGen[k] = false;
}
}
return true;
}
}
// gives output for line 2 till G.
void nextLine() {
for (j=0; j<G-1; j++){
for (k=0; k<L; k++){
if (nextGen[k] == true) {
System.out.print("*");
} else if (k==L-1) {
System.out.println("");
} else {
System.out.print(".");
}
}
}
}
void run() {
input();
line();
rules();
nextLine();
}
public static void main(String[] args) {
new Program().run();
}
}