0

I am trying to find duplicates values from the first column 0 in array that's been loaded in a string and split, and then throw an exception when this is found.

Here is my code.

public void loadTrueFalseQuestions() throws Exception {
    try {
        Scanner input = new Scanner(new FileInputStream(TRUE_FALSE_FILE));

        while (input.hasNextLine()) {
            line = input.nextLine();
            String[] split = line.split(",");
            int chapterNumber = Integer.parseInt(split[1]);
            String correctAnswer = (split[3]);
            String questionID = (split[0]);

            if (split.length != TRUE_FALSE_FIELDS) {
                throw new Exception();

            } else if ((!correctAnswer.matches("TRUE")) & (!correctAnswer.matches("FALSE"))) { //throw new Exception();
            } else if (true) {
              
            }

        }
    } catch (FileNotFoundException e) {

        System.out.println("Could not open a file.");
        System.exit(0);
    }

}

This is the CSV file.

TF001,8,Java allows an instance of an abstract class to be instantiated.,FALSE
TF001,8,Downcasting should be used only in situations where it makes sense.,TRUE
TF003,9,The throw operator causes a change in the flow of control.,TRUE
TF004,9,When an exception is thrown the code in the surrounding try block continues executing 
and then the catch block begins execution.,FALSE

I'm not sure how to go about it. I cannot get past the logic. I'm trying an if statement and contains(questionID) string method, but not sure how to combine these two. (if that makes sense).

Appreciate any advice.

4
  • The code you've shown checks whether the answer looks like "TRUE" or "FALSE". It does no comparison across lines at any point. What do you want to check for duplicates? A particular column? All columns? Commented Jul 6, 2021 at 14:07
  • You are checking the length of split after accessing split's members. You need to do that immediately after calling line.split. Commented Jul 6, 2021 at 14:08
  • If I understand your question correctly, it sounds like the string matching problem, I suggest taking a look at some string matching algorithms Commented Jul 6, 2021 at 14:34
  • @Silvio Mayolo - I wish to catch duplicates in column 0, and catch the exception. Commented Jul 7, 2021 at 1:12

1 Answer 1

2

I use a hash map and put the key field as the key in the hash map. You can check if you already put the key in the hash and if so do something because you already have it.

public void loadTrueFalseQuestions() throws Exception {
try {
    Scanner input = new Scanner(new FileInputStream(TRUE_FALSE_FILE));

    Map<String, String> myHash = new HashMap<>();

    while (input.hasNextLine()) {
        line = input.nextLine();
        String[] split = line.split(",");
        int chapterNumber = Integer.parseInt(split[1]);
        String correctAnswer = (split[3]);
        String questionID = (split[0]);

        if (myHash.containsKey(questionID)) {
            // what to do here?
        } else {
            myHash.put(questionID, line);
        }
    }
} catch (FileNotFoundException e) {

    System.out.println("Could not open a file.");
    System.exit(0);
}

}

Sign up to request clarification or add additional context in comments.

3 Comments

you can use myHash.put(split[0], line) == null
@Ofek you can, but I never check if the key isn't there. I always need to know if it is there to complete some method.
so use else or myHash.put(split[0], line) != null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.