Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to compare the values of 2 array indexes.

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Trying to compare the values of 2 array indexes.

    I am trying to make a trivia game program for my Java class at school. I am new to Java and I'm trying to compare the string value of an index in one array (player's answers) against the string value of an index in another array(answer key) but I have no idea what expression to use to do this.


    public static int player1_answer_check(String player1_answers) {
     
    		int player1_correct, index = 0, counter = 0;
     
    		do {
    			if (player1_answers[index].equals(Question.get_answer_key(counter))) {
    				player1_correct++;
    			}
    			else{
    			}
    			index++;
    			counter = counter + 2;		
    		}
    		while (counter < 10);
     
    		return player1_correct;
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to compare the values of 2 array indexes.

    To access an element in an array, use array notation. For a single dimension array: theArrayName[theIndex]
    I see that your code already uses array notation.

    An if statement could be used to compare two array elements.
    The current code uses an if statement.

    If the arrays hold objects, then use the equals method to compare.
    Again I see that your code already uses the equals method.


    Can you give some more details about your problem? Why doesn't the current code do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to compare the values of 2 array indexes.

    I'm using eclipse an it is showing me an error saying "The type of the expression must be an array type but it resolved to a string" when I hover over player1_answers[index].

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to compare the values of 2 array indexes.

    type of the expression must be an array type but it resolved to a string" when I hover over player1_answers[index].
    The variable: player1_answers is NOT defined as an array. Therefore the compiler will not allow array notation: [theIndex] to be used with it.
    player1_answers[index] The red part is not allowed for non-array variables.

    Where are the arrays you are talking about?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to compare the values of 2 array indexes.

    I figured it out, I didn't put the [] after the String with my argument.

Similar Threads

  1. STORING ALL INDEXES IN AN ARRAY USING RECURSion
    By prateek0112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2018, 05:51 AM
  2. Returning an array of indexes in my program.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 16th, 2013, 03:21 PM
  3. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  4. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM
  5. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM