#can not compare elements in an array list

1 messages · Page 1 of 1 (latest)

fair cipher
#

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();
    }
}

}
‘’’

unreal breachBOT
#

This post has been reserved for your question.

Hey @fair cipher! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fair cipher
#

My issue seems to be that the delimiter is not being implemented for its reading each line as 1 element so I can’t loop through it

unreal breachBOT
#
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();
        }
    }
}
‘’’