简体   繁体   中英

How do I compare certain characters in arrays?

I am trying to make a project for school where I have to guess a word. If the word is right, it gets a red dot and if it's in there somewhere, it will get a yellow dot and if there is no similarity, it will get a gray dot. I have made an array that contains the word that has to be guessed that's named array and an array that contains the word being guessed having one character in each array entry.

I thought some setup like this would be viable:

place = 0;   // The tekenaarextra and checkletters are not the problem that's just the result that happens if they are true

while(place <5){
    if(array[place] == array2[place]){
        System.out.println(array[1]);
        checkletters(place,1,Color.RED);
        tekenaarextra2();
        place ++;
    }else if( array[0] == array2[place] || array[1] == array2[place] || array[2] == array2[place] || array[3] == array2[place] || array[4] == array2[place] ){ 
        checkletters(place,1,Color.YELLOW);
        tekenaarextra2();
        place ++;
    }else{
        checkletters(place,1,Color.GRAY); // this is just a method to draw the dots.
        tekenaarextra2();
        place ++;
    }
}

However, whilst using this I get error messages and I do not quite know how to really compare separate entries from the arrays without putting them into different Strings which is a lot of separate work.

For example, I got String[] array = {a,p,p,l,e} , and I got String[] array2 = {p,l,a,t,e}

I want to then compare the first entry from the array2 to the first one of the array and if they're the same character, I should execute a certain command to draw a dot if it's not so, else if it should compare the first entry from array 2 to all the other entries to see if it needs a yellow dot. If it all tests false and it doesn't contain any of them, it should just have an else that leads into drawing a gray dot. And this over 5 times to compare all the letters of array 2, but that can be done simply by a while statement.

Simplified version of what I need to do

if(array[0]==array2[0]){ // if the first letter of guessed word is the  first letter in the mystery word
  drawreddot();
} else if( array[0] == array2[1] || array[0] == array2[2] || array[0] == array2[3] || array[0] == array2[4]){ 
// if the letter guessed isn't in the same place but in the mystery word on another place
draw yellow dot
}else{ // if it doesn't compare to any of the entries
draw gray dot
}
import java.util.*;
import java.util.stream.*;
public class DotIt {
    public static void main(String[] args) {
        String mysteryWord = "apple";
        char[] mysteryChars = mysteryWord.toCharArray();
        String guessedWord = "plate";
        char[] guessedChars = guessedWord.toCharArray();
        List<Integer> mysteryCharList = mysteryWord.chars().boxed().collect(Collectors.toList());

        System.out.println("Mystery word: " + mysteryWord);
        System.out.println("Guessed word: " + guessedWord);

        for (int i = 0; i < mysteryChars.length; i++) {
            if (guessedChars[i] == mysteryChars[i]) {
                // letter of the guessed word is at same positioon in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is red.");
            } else if (mysteryCharList.indexOf(Integer.valueOf(guessedChars[i])) != -1) {
                // the letter of the guessed word is in the mystery word at on another place
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is yellow.");
            } else {
                // the letter of the guessed word isn't in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is grey.");
            }
        }
    }
}
$ java DotIt.java
Mystery word: apple
Guessed word: plate
letter 1 ('p') is yellow.
letter 2 ('l') is yellow.
letter 3 ('a') is yellow.
letter 4 ('t') is grey.
letter 5 ('e') is red.
$ 

if input array is string array try this below code.

private void guess(String[] guessed, String[] original){
    Set<String> set = new HashSet<>();
    for(String o : original){
        set.add(o);
    }
    for (int i = 0; i < guessed.length; i++) {
        if(guessed[i].equals(original[i])){
            System.out.println("GREEN");
        }
        else if(set.contains(guessed[i])){
            System.out.println("YELLOW");
        }
        else{
            System.out.println("GREY");
        }
    }
}

my main method

public static void main(String[] args) {
    Solution solution = new Solution();
    solution.guess(new String[]{"a", "p", "p", "l", "e"}, new String[]{"p", "p", "a", "l", "x"});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM