0

I am working on very simple program to split characters from the word and ask user to input character. IF user input character match the characters array then display "YES" if not then display "NO". I used .ToCharArray method and loop to split each character from word and assign it to character array. Then I used for loop with IF statement to check the condition and display the result. But it matches with only one character from character array and ignores other.

public class test {


public static void main(String[] args) {

    // Declaring variables
    String[] wordsList= {"java"};
    char[] wChar = wordsList[0].toCharArray();
    char wCharLetter = 0;
    char inputChar;

    Scanner input = new Scanner (System.in);

    for (int i = 0; i < wordsList[0].length(); i++){
        wCharLetter = wChar[i];
    }

    for (int i = 0; i < wordsList[0].length(); i++){
        inputChar = input.next().charAt(0);
        if (inputChar==wCharLetter){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
    }       

} }

According to my understanding; technically the wCharLetter variable should store all the characters, and it does when I print wCharLetter but doesn't work in matching.

2
  • 1
    The wCharLetter variable only stores the last character, because with each assignment you are overwriting the previous value. Commented Oct 29, 2015 at 17:19
  • 1
    In what way do you think one letter the same as multiple letters? Commented Oct 29, 2015 at 17:25

2 Answers 2

1

Simply assign wCharLetter = wChar[i]; within your second for loop and ditch the previous loop:

for (int i = 0; i < wordsList[0].length(); i++) {
    wCharLetter = wChar[i];
    inputChar = input.next().charAt(0);
    if (inputChar == wCharLetter) {
        System.out.println("YES");
    } else {
        System.out.println("NO");
    }
}

Also, don't forget to close your Scanner once done.

// ...
input.close();

Possible input/outputs:

j
YES
a
YES
v
YES
a
YES

... or...

l
NO
a
YES
v
YES
a
YES
Sign up to request clarification or add additional context in comments.

2 Comments

It works when characters are entered in the same order of word characters. j a v a (works) but if enter: a v j a (doesn't work then). Thanks for help
@MaihanNijat yes that's normal. If you want it to work regardless of order, you'll need to ditch that code pretty much entirely and collect the characters in one list, then the input in another, then compare the two lists. You'll also need a breaking case for when the user is supposed to stop adding characters.
0

Check this out:

String wordArray="Java";

    Scanner input = new Scanner (System.in);



        for (int i=0;i<=wordArray.length();i++)
        {
            if(wordArray.charAt(i)==input.next().charAt(0))
            {
                System.out.println("YES");
                break;
            }
            else
            {
                System.out.println("NO");
                break;
            }
        }

    input.close();

Hope this will help.

Comments

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.