Every round on this table is the same task as Problem 277: you're handed a stack of cards and one target rank, and you count matches. That's a single loop with a counter:
int count = 0;
for (int i = 0; i < cards.length; i++) {
if (cards[i].equals(targetRank)) {
count++;
}
}
System.out.println(count);
Two traps worth knowing before you submit real code:
1. Ranks come in as Strings ("2" through "10", or "Jack"/"Queen"/"King"/"Ace") — compare them with .equals(), never ==. String == compares object references, not contents, and it will pass your own quick tests while quietly failing on the judge.
2. The problem gives you multiple test cases per run (a count on line 1, then that many test cases in sequence) — your outer loop needs to read and print once per test case, not just once total.
Sample from the original problem, for reference:
Input:
2
5
2
King
Ace
King
Jack
King
4
2
3
4
5
4
Output:
2
1
Once you've played a few rounds here and trust your counting logic, head to the Code Quest site and submit it for real in Java.
The Codio skeleton hands you the hand of cards as a List<String> (specifically an ArrayList) instead of a plain array, because you don't know how many cards are coming until you read N from the file. An array needs its size locked in up front; an ArrayList grows as you add to it.
Creating one and adding to it:
List<String> cards = new ArrayList<>();
cards.add("King");
cards.add("5");
cards.add("Jack");
The <String> is the type it holds — this ArrayList only holds Strings. That's exactly what the skeleton does in the reading loop: it starts with an empty list and calls .add() once per card it reads from the file.
Finding out how many items are in it:
int howMany = cards.size(); // 3, not cards.length -- ArrayLists use size(), arrays use .length
Getting one item out by position:
String first = cards.get(0); // "King" -- indexes still start at 0
Walking through every item (the version you'll actually want here):
// for-each -- reads "for each String c in cards"
for (String c : cards) {
System.out.println(c);
}
// the equivalent classic for loop, if you'd rather index it
for (int i = 0; i < cards.size(); i++) {
System.out.println(cards.get(i));
}
Either loop visits every card exactly once, in order. For counting how many match a target rank, the for-each version reads the cleanest: check each c against your target inside the loop, same idea as looping over an array, just with .size() instead of .length and no [i] brackets required.
The one rule that still applies: the items inside are Strings, so compare them with c.equals(targetRank), never c == targetRank — that doesn't change just because the Strings now live inside an ArrayList instead of an array.
That's really all countRank() needs: a for-each loop, an .equals() check, and a counter you increment. If you get stuck, try writing it first as an array version in your head, then translate array.length → list.size() and array[i] → list.get(i).