1

EDIT: IT'S FIXED, THANKS FOR THE HELP!

So basically I have an array of strings, a question and an answer

public static String[][] triviaData = { 
    {"Question2", "Answer1"},
    {"Question2", "Answer2"},
    {"Question3", "Answer3"},
};

And I am trying to make a method that validates an entered input, lets call the entered input enteredAnswer. enteredAnswer is stored as a String.

I am trying to validate that the enteredAnswer is the same as the second index of the array.

if (enteredAnswer.equalsIgnoreCase(triviaData[Config.CurrentQuestion][1])) {

This is the code I tried, but I get the error "Cannot invoke equalsIgnoreCase(String) on the array type String[]"

I am a beginner programmer so if you could help me out it would be highly appreciated. Thanks.

enteredAnswer is stored as

public String[] enteredAnswer;
2
  • 1
    Please show the declaration of the enteredAnswer variable. Commented Sep 2, 2012 at 3:28
  • Glad we could help; be sure to accept an answer that helped you the most. It lets the community clearly know that you have your answer. Commented Sep 2, 2012 at 3:40

2 Answers 2

1

The reason your code invocation isn't working is because enteredAnswer is a String[], not a String. Specify which element you want to use to get the String you want.

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

Comments

1

enteredAnswer is an array it appears. You need a String to call equalsIgnoreCase on, could you show a little more code?

Also, It'd probably be better to use something like a HashMap to store questions + answers. You could do this like so:

HashMap<String, String> questionAnswers = new HashMap<String, String>();

And later

questionAnswers.put("Blah blah?", "blah.");

And to check if an answer to a given String question String question with answer String answer just do:

String realAnswer = questionAnswers.get(question);
boolean correct = realAnswer.equalsIgnoreCase(answer);

1 Comment

it's not an array, it's stored as public String[] enteredAnswer, the array it's referring to is triviaData also thanks, I will look into hashmap but for now its only using a few questions.

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.