This question has already been answered:

Why doesn't the index of the found word appear on the console?

The user enters the word. The program searches the array and displays its index:

import java.util.Scanner; public class MassiveStrings { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.print("Введите слово: "); String object = scn.nextLine(); String[] word = { "Масло", "Вода", "Дерево", "Кефир", "Помидор", "Сельдерей", "Кувшин" }; for (int i = 0; i < word.length; i++) { if (word[i] == object) System.out.print("Ваш индекс" + i); } } } 

Marked as a duplicate by the participants of zRrr , Anton Shchyrov , Community Spirit 24 Apr '18 at 15:29 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    To compare strings in Java, you need to use the .equals() method:

     if (word[i].equals(object)) 

    And word[i] == object compares the same word[i] object and object or different. Obviously different.

    • 2
      Where you generally saw the isEquals method - there is no such method in java , there is an equals() method - And
    • @And Yes, I myself have no idea where it came from in my head. Already fixed :) - Enikeyschik
    • isEmpty is, but not isEquals - Sanaev
    • Thank you all for the lightning-fast help :)! - Nikita Telegin
    • @And isEqual() is in the LocalDateTime class - Enikeyschik