简体   繁体   English

如何检查符号是否在用户输入上?

[英]How can I check if the symbol is on the user input?

I tried searching for an answer, but I don't know how to answer it.我试图寻找答案,但我不知道如何回答。 I tried making my own, but it show me this error:我试着自己做,但它告诉我这个错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method contains(CharSequence) in the type String is not applicable for the 
arguments (char)

I don't know why it says charsequence when I just used a simple array.当我只使用一个简单的数组时,我不知道为什么它说字符序列。 The purpose of this part of a code is like a checker for invalid inputs.这部分代码的目的就像检查无效输入。 Here is the code:这是代码:

                for(int i = 0; i <= 9; i++) {
                    if(GN.contains(Integer.toString(i))){
                        checked = false;
                        if(!checked) {
                            System.out.println("Invalid type, try again.");
                            main.GivenName();
                        } else {
                            String InvalidSym = "`!@#$%^&*()_=+{}[]|\";:\\?";
                            char[] FinalCheck = new char[InvalidSym.length()];
                            for(int j = 0; j <= FinalCheck.length; j++) {
                                  FinalCheck[j] = InvalidSym.charAt(j);
                            }
                            for(int k = 0; k <= FinalCheck.length; k++) {
                                if(GN.contains(FinalCheck[k])) {
                                    checked = false;
                                }
                            }
                        }
                    }
                } //second checker

FinalCheck is a char array which doesn't implemented CharSequence interface FinalCheck是一个未实现CharSequence接口的字符数组

so you can try所以你可以试试

if(GN.contains(String.valueOf(FinalCheck[k])))

or you can convert GN into char array once and write a method to check if FinalCheck[k] is in GN array或者您可以将GN转换为 char 数组一次并编写一个方法来检查FinalCheck[k]是否在 GN 数组中

contains methods is not applicable for char value, the last for loop should contains this if statement: contains 方法不适用于 char 值,最后一个 for 循环应包含以下 if 语句:

  for(int k = 0; k <= FinalCheck.length; k++) {
     if(GN.contains(String.valueOf(FinalCheck[k]))) {
         checked = false;
      }
  }

on your code the FinalCheck[k] is a char value as the type of the FinalCheck array is char[].在您的代码中,FinalCheck[k] 是一个 char 值,因为 FinalCheck 数组的类型是 char[]。 using the valueOf will convert this char value to String value.使用 valueOf 会将这个 char 值转换为 String 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM