-1

I have the following code:

import java.util.Scanner;
public class Chapter10_NextGeneration {
public static void main(String[]args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Type in your cell positions(ie 8,A,D,D,A,D,D,A,A,2 - NO SPACES & CASE SENSITIVE): ");
    String positions_str=sc.nextLine();
    sc.close();
    String[] cells=positions_str.split(",");
    String[] next_generation=new String[cells.length-2];
    for(int j=0; j < Integer.parseInt(cells[cells.length-1]); j++) {
    for(int i=1;i<cells.length-1;i++) {
        if(i-1<1) { 
            if(cells[i+1].equals("D")) {
                next_generation[i-1]="D";
            }
            else {
                next_generation[i-1]="A";
            }
        }
        else if(i+1==cells.length-1) {
            if(cells[i-1].equals("D")) {
                next_generation[i-1]="D";
            }
            else {
                next_generation[i-1]="A";
            }
        }
        else if((cells[i-1].equals("A") && cells[i+1].equals("D")) || (cells[i-1].equals("D") && cells[i+1].equals("A"))) {
                next_generation[i - 1]="A";
            }
        else if((cells[i-1].equals("A") && cells[i+1].equals("A")) || (cells[i-1].equals("D") && cells[i+1].equals("D"))) {
                next_generation[i - 1]="D";
            }
    }
        for (int i = 0; i < next_generation.length; i++) {
            cells[i+1]=next_generation[i];
            System.out.print(next_generation[i]);
        }
    }
}

}

I have tried and tried to fix my code, but I can't fix my code to say, AAADADDA. It keeps printing, DAADAAAAAAADADDA. Is there something wrong with my for loops? Or is it something with the content of the loops?

boi yeet
  • 86
  • 10

4 Answers4

1

Try to use this!

String nextGenString = String.join(" ", next_generation);

Example:

String [] letters = {"H","E","Y"};
String nextGenString = String.join(" ", letters);
System.out.print(nextGenString); //prints H E Y
Rauññ
  • 378
  • 3
  • 17
1

Using String.join():

System.out.println(String.join("", next_generation));

Using Arrays.stream().collect():

System.out.println(Arrays.stream(next_generation).collect(Collectors.joining()));
0
StringBuilder str = new StringBuilder();
for(String ng : next_generation)
{
    str.append(ng);
}
System.out.println(str.toString());
adxl
  • 829
  • 1
  • 9
  • 19
0

You can't directly convert the String[] to string using the function toString().You need to take help of StringBuilder

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Type in your cell positions(ie 8,A,D,D,A,D,D,A,A,2 - NO SPACES & CASE SENSITIVE):");
        String positions_str = sc.nextLine();
        sc.close();
        String[] cells = positions_str.split(",");
        String[] next_generation = new String[cells.length - 2];
        for (int i = 1; i < cells.length - 1; i++) {
            if (cells[i - 1].equals("") || cells[i + 1].equals("")) {
                cells[i - 1] = "D";
                cells[i + 1] = "D";
                if ((cells[i - 1].equals("A") && cells[i + 1].equals("D")) || (cells[i - 1].equals("D") && cells[i + 1].equals("A"))) {
                    next_generation[i] = "A";
                }
                if ((cells[i - 1].equals("A") && cells[i + 1].equals("A")) || (cells[i - 1].equals("D") && cells[i + 1].equals("D"))) {
                    next_generation[i] = "D";
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : next_generation) {
            stringBuilder.append(string);
        }
        System.out.println(stringBuilder.toString());
    }

Hope so it works for you.

Lily
  • 605
  • 3
  • 15
  • 31