Check for value in array

前端 未结 4 1895
时光说笑
时光说笑 2021-01-29 03:14

I need to check the array to see if the user input is already present, and display a message as to whether it is or isn\'t there. The first part is working, but I tried to creat

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 04:02

    You're going to have to loop through the entire array and check if scan.next() equals any of them - if so return true - as such:

    String toCheck = scan.next();
    for (String string : i) { //For each String (string) in i
        if (toCheck.equals(i)) {
            System.out.println("The word has been found");
            return;
        }
    }
    System.out.println("The word has not been found");
    

    This supposes you call WordCheck(), passing the array to it - this method also has to be static for you to call it from the main() method.

提交回复
热议问题