This Java code involves reading in a file and comparing whether three card decks are equal. A set of cards is only valid if non of their attributes match or all their attributes match.
For example a file may look like this:
Sample File ('cards.txt'):
square,blue,spot circle,red,solid triangle,green,stripe
square,blue,spot square,blue,spot triangle,green,stripe
Sample I/O:
Enter the name of the cards file:
cards.txt
Processing: square,blue,spot circle,red,solid triangle,green,stripe
Valid
Processing: square,blue,spot square,blue,spot triangle,green,stripe
Invalid
Done
‘’’
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Check {
public static void main(String[] args) {
ArrayList<String> cards = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the cards file:");
String command = sc.nextLine();
File text = new File(command);
try (Scanner file = new Scanner(text)) {
while(file.useDelimiter(",").hasNextLine()){
cards.add(file.useDelimiter(",").nextLine());
System.out.println("Processing: " + cards.toString().replace("[", "").replace("]", ""));
for (int i = 0; i < cards.size(); i++) {
for (int j = i+3; j < cards.size(); j++) {
// compare cards.get(i) and cards.get(j)
if(cards.get(i) == cards.get(j)){
System.out.println("Valid");
}else{
System.out.println("Invalid");
}
}
}
System.out.println(cards.size());
cards.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
‘’’