I'm currently doing a practice question for an upcoming practical exam and am stuck. I've written a method to check whether a set of three cards is valid or invalid where we're told that "Two cards are distinct if they do not have the same shape or colour or pattern i.e. none of their attributes match. Two cards are the same if they have the same shape and colour and pattern i.e. all their attributes match. A set of three cards is valid if it consists of (i) three distinct cards, or (ii) three of the same card.". Currently, if I run my program, every set of three cards is evaluated as "valid". I'm pretty sure the problem doesn't lie in the way I read in and store the cards and their attributes because I've debugged that so I'm assuming the problem lies in my logic for my cardsAreValid method, and specifically with how I compare the different string elements (attributes) for each card. However, I could be wrong. Please help.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Check {
public static boolean cardsAreValid(String [][] threeCards){
boolean sameShape = threeCards[0][0].equals(threeCards[1][0]) && threeCards[1][0].equals(threeCards[2][0]);
boolean sameColour = threeCards[0][1].equals(threeCards[1][1]) && threeCards[1][1].equals(threeCards[2][1]);
boolean samePattern = threeCards[0][2].equals(threeCards[1][2]) && threeCards[1][2].equals(threeCards[2][2]);
if (sameShape == true && sameColour==true && samePattern==true) // if all attributes match - same so valid
return true;
else if (sameShape==false && sameColour==false && samePattern==false) // if no attributes match - distinct so valid
return true;
else
return false; // if only 1 or 2 cards differ/match its invalid
}
public static void main (String [] args) {
File inFile = new File("cards.txt");
String [][] cards = new String[3][3];
List<String> allLines = new ArrayList<>();
try (Scanner sc = new Scanner(inFile)) {
while (sc.hasNextLine()) {
allLines.add(sc.nextLine());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < allLines.size(); i++) {
String fullLine = allLines.get(i);
System.out.println("Processing: " + fullLine);
String [] singleCard = fullLine.split(" ");
for (int j = 0; j < 3; j++) {
int firstComma = singleCard[j].indexOf(",", 0);
int secondComma = singleCard[j].indexOf(",", firstComma+1);
String shape = singleCard[j].substring(0, firstComma);
String colour = singleCard[j].substring(firstComma+1, secondComma);
String pattern = singleCard[j].substring(secondComma+1);
cards[j][0] = shape;
cards[j][1] = colour;
cards[j][2] = pattern;
}
if (cardsAreValid(cards) == true) {
System.out.println("Valid");
}
else
System.out.println("Invalid");
}
}
}
Just incase, the sample text file cards.txt looks as follows:
square,blue,spot circle,red,solid triangle,green,stripe
square,blue,spot circle,red,solid triangle,green,solid
square,blue,spot square,blue,spot square,blue,spot square,blue,spot
square,blue,spot triangle,green,stripe
Yes, you have a problem with our logic. You are currently checking if there are two distinct cards within your cards, so if
card1
is distinct fromcard2
, but the same ascard3
, you will getvalid
. because all three variables (sameShape
,sameColour
,samePattern
) will be false.Try modifying your logic so it will check that none of the cards are equal. You might want to make a help function that will get three string and checks if none of them are equal.