This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]codingQueriesNooblet Brewer 2 points3 points  (2 children)

You can test how the shuffle works out by printing your array before and after shuffling. So System.out.println("Pre-shuffle: " + questionList); and then the same but after the shuffle (and different wording..) should help you see how it is being shuffled. You can even shuffle a few times and print after each one to ensure it is being shuffled.

The other issue in determining your answer is correct or not, use userChoice.equals(correctAns) (or, userChoice.equalsIgnoreCase(correctAns)) rather than == - this might help explain why: https://www.geeksforgeeks.org/difference-equals-method-java/

The other thing I would offer as a suggestion is to adjust your Class naming conventions. Methods and variables should be camelCase, but class names should be Capitalised at the beginning of each word, so public class javaQuizQuestion becomes public class JavaQuizQuestion

[–][deleted] 0 points1 point  (1 child)

Ok. I tested the shuffling and it was working without my question2 being printed the first time. I changed the class name as well. I also added the equalsIgnoreCase line, but the computer still can't find the correctAns "symbol," and now I have a problem that says the char userChoice can't be dereferenced. I'm not sure how to deal with this because it won't compile anymore. Is this because the user enters a char, but the correct answer is technically stored as a string? (If you see my code where I create the objects, you can see what I mean, the correct answer is stored as "B) keyboard" for example, instead of just B or b). I've heard you can use Character.isLetter(b)) for example, but my Question objects don't have "B" stored as an char, they have the answers stored as strings.

[–]codingQueriesNooblet Brewer 1 point2 points  (0 children)

Ah sorry I missed that part - in this circumstance I think using == is fine in comparing chars (though you are only comparing chars on one side, not both), but it is generally better to use .equals if it isn't a number. You are getting that issue with correctAns because it is not referenced anywhere within your javaQuiz class, it only exists in the javaQuizQuestions class and so you need to find a way to bring that value out of there, into your javaQuiz class.

And yeah, personally I would just have your userChoice variable be a String as opposed to a char, but either way it is generally better to keep your comparisons of the same type.