簡體   English   中英

如何檢查字符串是否包含數組中的值

[英]How to check if String contains values from an array

我是 Java 的新手,正在嘗試檢查用戶名是否有臟話。 我制作了一個包含 4 個臟話的數組,現在我想檢查用戶輸入的臟話,但是,我不知道如何形成 if 語句來檢查數組中的所有項目。

    public static void main(String[] args) {

    Scanner character = new Scanner(System.in);

    String[] profanities = {"asshole", "ass", "idiot", "stupid"};


    System.out.println("What is your name");
    String userName = character.next();
    if (userName.contains(profanities[])) { //This Part is what i dont understand
        System.out.println("Invalid name");
    }
    else {
        System.out.println("Valid Name!");
    }
}

使用 Set 而不是 ArrayList ,然后使用 profanities.contains profanities.contains(userName) 請注意,用戶應該輸入與褻瀆集中的確切褻瀆語言,以便 if 語句評估為真。 如果用戶輸入類似“userjackass”的內容,則不會被歸類為褻瀆。

無需再創建Collections ,只需您的原始數組即可。 由於無效名稱集之前已設置/已知,因此sort操作只能在程序啟動時執行一次。

排序后,只需為每個輸入調用binarySearch即可:

Arrays.sort(profanities); //--> if profanities is a static set, call this just once.

if (Arrays.binarySearch(profanities,username)>=0)  
    System.out.println("Invalid name");
else 
    System.out.println("Valid Name!");

//binarySearch will return >=0 if the value is found

這避免了創建集合、列表或循環的實現。

如果無效名稱集可能發生變化(例如添加新名稱),則需要進行第二次sort操作。 在這種情況下,提供的其他答案將是更好的方法 如果無效名稱集已知且在程序執行期間不會更改,請使用此方法。

我會這樣做..

import java.util.Scanner;

public class Profanities {

    public static void main(String[] args) {

        boolean allow = false;
        Scanner scan = new Scanner(System.in);
        String[] profanity_list = {"asshole", "ass", "idiot", "stupid"};
        String test_input = "";

        // do while until allow equals true..
        while (!allow) { 

            System.out.println("What is your name?");
            test_input = scan.nextLine();
            byte memory = 0;

            // compare the input with the elements of the array..
            for (int pos = 0; pos < profanity_list.length; pos++) {

                if (test_input.contains(profanity_list[pos]) == true) {
                    System.out.println("Invalid name. Let's start again..");
                    memory = 1;
                    break;
                }
            }
            // if memory equals 1, it means that a profanity was found..
            if(memory == 0){allow = true;}
        }
        String name = test_input;
        System.out.println("That is a valid name. Thanks.");
    }
}

嘗試:

for(String string : profanities)
  if(userName.contains(string))System.out.println("invalid name"); 
  else System.out.println("valid name");

注意: for循環遍歷數組中的每個條目並檢查該條目是否包含在 userName 中

不要使用字符串數組,而是使用字符串列表,您可以從中輕松使用 contains 方法。 我用列表修改了你的代碼,參考下面

public static void main(String[] args) {

    Scanner character = new Scanner(System.in);

    //String[] profanities = {"asshole", "ass", "idiot", "stupid"};
    
    ArrayList<String> profanities = new ArrayList<String>();
    profanities.add("asshole");
    profanities.add("ass");


    System.out.println("What is your name");
    String userName = character.next();
    if (profanities.contains(userName)) { //This Part is what i dont understand
        System.out.println("Invalid name");
    }
    else {
        System.out.println("Valid Name!");
    }
}

使用 List 的另一個優點是,它是動態的,您可以在將來向其中添加元素而不會出現任何問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM