I’m trying to count the number of characters that occur in a java string.
For example:
given the poker hand 6s/3d/2H/13c/Ad
how many times does the / character occur? = 4
The user can enter a different hand with a changing number of card variables so hardcoding a method to check for occurrences isn’t going to work.
A separator may be any one of: – / space (with only one separator type allowed to be used in one hand).
So I need to be able to check if either of the separators occurs 4 times otherwise the incorrect format has been given.
Here’s a some java code to give a better idea of what I’m trying to do:
String hand = "6s/1c/2H/13c/Ad";
System.out.println("Original hand: " + hand);
// split the hand string into individual cards
String[] cards = hand.split(hand);
// Checking for separators
// Need to check for the correct number of separators
if(hand.contains("/")){
cards = hand.split("/");
} else if (hand.contains("-")){
cards = hand.split("-");
} else if (hand.contains(" ")){
cards = hand.split(" ");
} else {
System.out.println("Incorrect format!");
}
Any help would be great!
Also this is a school project/homework.
Edit 1——————————————————–
OK so here’s my code after your suggestions
String hand = "6s 1c/2H-13c Ad";
System.out.println("Original hand: " + hand);
// split the hand string into individual cards
String[] cards = hand.split("[(//\\-\\s)]");
if (cards.length != 5) {
System.out.println("Incorrect format!");
} else {
for (String card : cards) {
System.out.println(card);
}
}
The given hand above is not in the correct format because the user can only use ONE type of separator for a given hand. For example:
- 6s/1c/2H/13c/Ad – correct
- 6s-1c-2H-13c-Ad – correct
- 6s 1c 2H 13c Ad – correct
How do I ensure that the user only uses ONE type of separator??
Cheers for the answers so far!
Edit 2 ——————————————
So playing around with nested if statements my code now looks like this:
String hand = "6s/1c/2H/13c/Ad";
System.out.println("Original hand: " + hand);
// split the hand string into individual cards
if(hand.contains("/")){
String[] cards = hand.split("/");
if(cards.length != 5){
System.out.println("Incorrect format! 1");
} else {
for (String card : cards) {
System.out.println(card);
}
}
} else if(hand.contains("-")){
String[] cards = hand.split("-");
if(cards.length != 5){
System.out.println("Incorrect format! 2");
} else {
for (String card : cards) {
System.out.println(card);
}
}
} else if(hand.contains(" ")){
String[] cards = hand.split(" ");
if(cards.length != 5){
System.out.println("Incorrect format! 3");
} else {
for (String card : cards) {
System.out.println(card);
}
}
} else {
System.out.println("Incorrect format! 4");
}
This way works as intended but is ugly!
Any suggestions would be great cheers.
I wrote this before the first question edit, so I am responding to the original question and not its addendum question.
In the documentation on String.split, it is unclear whether empty strings count as substrings. Notice that
"--".split("-").length == 0. The question may implicitly guarantee that two or more characters separate delimiters, but that is a risky assumption and where Java’s String.split becomes problematic.This is a partial simpler implementation:
Full code follows, with editorial comments intended for homework.
Just for education, the regular expression approach can be good. The entire solution takes one line of Ruby,
hand.split(/[\-\/ ]/, -1).length - 1.