0

I'm trying to check if a letter submitted by user is contained in a string, but it always return false. From what I read the .equals() function should work. I was expecting that if a user inputs the letter "a" it would return "pass" if the string was "america".

 for (int i = 0; i < outputTest.length(); i++){
                    if (userInput.equals(outputTest.charAt(i))){
                        System.out.println("Pass");
                    }else {
                        System.out.println("Fail");
                    }
                }
Jeann Pierre
  • 131
  • 3
  • 11

4 Answers4

1

Based on outputTest.length() and outputTest.charAt() I am assuming that outputTest is String.
Based on userInput.equals I am assuming it is not primitive type like char (since primitive types don't have methods). It is also not Character, otherwise you would see Pass few times. So it most likely is also String.


outputTest.charAt(i) returns char, but you are comparing it with String which equals method looks like:

964  public boolean equals(Object anObject) {
965 if (this == anObject) {
966 return true;
967 }
968 if (anObject instanceof String) {
969 String anotherString = (String)anObject;
970 int n = value.length;
971 if (n == anotherString.value.length) {
972 char v1[] = value;
973 char v2[] = anotherString.value;
974 int i = 0;
975 while (n-- != 0) {
976 if (v1[i] != v2[i])
977 return false;
978 i++;
979 }
980 return true;
981 }
982 }
983 return false;
984 }

So since equals expects Object, char (returned from outputTest.charAt(i)) will be automatically boxed to Character, but since Character doesn't extend String test

if (anObject instanceof String) 

will fail and you will immediately move to return false;.


You may want to use contains method if you want to check if one String contains another

outputTest.contains(userInput)
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

To find a string in another string, just do:

return text.contains(string);

To find a char in a string, do

return text.indexOf(ch) > -1;
Daniel Rodriguez
  • 607
  • 6
  • 11
0

What is the data type of your object userInput? You seem to be comparing a String object with a character which is returned by the charAt() method. That will always return false.

Bala_G
  • 1
  • 1
0

When you are learning to use methods, a good habit to develop is to look at what kind of data type the methods apply or return. For instance, you cannot code this: char scanner.nextLine() // nextLine() is to read String, not char. Therefore, in your code outputTest.charAt(i) // I am a char, and this userInput.equals() // I am a String, you are comparing different things. You cannot compare an apple with a desk because they are two different objects. The compiler will always tell you false.

I assume: String inputTest = scanner.nextLine(); for user input a letter, and you can consider "a" as a String with a length 1. (or any other letters inside)

    if (outputTest.contains(userInput)) // now you are comparing String and String
       System.out.println("Pass");
    else
       System.out.println("Fail");
Bruce Lin
  • 76
  • 5