1

Hoping someone could help me out with something here, I'm trying to split a long string w/ numbers and card suits so that it displays nicely by number.

AS AC AH AD 2S 2C 2H 2D 3S 3C 3H 3D 4S 4C 4H 4D 5S 5C 5H 5D 6S 6C 6H 6D 7S 7C 7H 7D 8S 8C 8H 8D 9S 9C 9H 9D 10S 10C 10H 10D JS JC JH JD QS QC QH QD KS KC KH KD 

would like it to split like:

AS AC AH AD
2S 2C 2H 2D
3S 3C 3H 3D

etc...

Is there a way to use .split() every certain number of characters,etc or by next number?

below is my code to generate a deck of cards

public class Main {
    public static void main(String[] args) {
        //System.out.printf("hello world");

        String cards = "";
        char[] suits = {'S', 'C', 'H', 'D'};
        for(int i = 1; i <=14; i++){
            for(int j = 0; j < suits.length; j++){
                if(i == 1){
                    cards = cards + 'A' + suits[j] + " ";
                } else if(i == 11){
                    break;
                } else if(i == 12){
                    cards = cards + 'J' + suits[j] + " ";
                } else if(i == 13){
                    cards = cards + 'Q' + suits[j] + " ";
                } else if(i == 14){
                    cards = cards + 'K' + suits[j] + " ";
                } else {
                    cards = cards + i + suits[j] + " ";
                }
            }
        }
        System.out.println(cards);
    }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
gsomBL89
  • 11
  • 1
  • it doesnt format how id like it in the question, so i want it to start a new row with each new number – gsomBL89 Apr 13 '21 at 15:09
  • https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#insert(int,%20char) – Oleks Apr 13 '21 at 15:17

4 Answers4

2

Actually, in your case, you want to split on the space following a D, so split("(?<=D) ") will do:

String input = "AS AC AH AD 2S 2C 2H 2D 3S 3C 3H 3D 4S 4C 4H 4D 5S 5C 5H 5D 6S 6C 6H 6D 7S 7C 7H 7D 8S 8C 8H 8D 9S 9C 9H 9D 10S 10C 10H 10D JS JC JH JD QS QC QH QD KS KC KH KD";
String[] ranks = input.split("(?<=D) ");
for (String rank : ranks)
    System.out.println(rank);

Output

AS AC AH AD
2S 2C 2H 2D
3S 3C 3H 3D
4S 4C 4H 4D
5S 5C 5H 5D
6S 6C 6H 6D
7S 7C 7H 7D
8S 8C 8H 8D
9S 9C 9H 9D
10S 10C 10H 10D
JS JC JH JD
QS QC QH QD
KS KC KH KD
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

The simplest way is to include a newline character \n after each value like this:

for(int i = 1; i <=14; i++){
  for(int j = 0; j < suits.length; j++){
    // your stuff
  }
  cards = cards + '\n';
}

Note however, that concatenating to a String in a loop is usually discouraged in Java, because it requires constructing an entirely new String object for each concatenation, thus leading to lots of copying (which could be bad for performance and GC pressure). Use of a StringBuilder is often considered better.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

A possible solution is that you will first split your string by space (" ").

Then loop through the resulting array, keeping track of the latest "starting character" (eg. 2).

Print the new item as long as it startsWith() that character. If it doesn't, print a new line and change the "starting character" (eg. 3)

Tiberiu
  • 990
  • 2
  • 18
  • 36
0

You may have to bend this if the format changes, but in your current example printing a new line after 4 items is just fine:

final String testCase = "AS AC AH AD 2S 2C 2H 2D 3S 3C 3H 3D 4S 4C 4H 4D 5S 5C 5H 5D 6S 6C 6H 6D 7S 7C 7H 7D 8S 8C 8H 8D 9S 9C 9H 9D 10S 10C 10H 10D JS JC JH JD QS QC QH QD KS KC KH KD";
final String[] arr = testCase.split(" ");
for(int i = 0; i < arr.length; i++) {
    System.out.print((i % 4 == 0 ? "\n" : " ")+arr[i]);
}

Which prints:

AS AC AH AD
2S 2C 2H 2D
3S 3C 3H 3D
4S 4C 4H 4D
5S 5C 5H 5D
6S 6C 6H 6D
7S 7C 7H 7D
8S 8C 8H 8D
9S 9C 9H 9D
10S 10C 10H 10D
JS JC JH JD
QS QC QH QD
KS KC KH KD
Spectric
  • 30,714
  • 6
  • 20
  • 43