【问题标题】:Check if String contains Numeric value and Alpha value检查字符串是否包含数值和 Alpha 值
【发布时间】:2018-07-29 07:38:08
【问题描述】:

我正在尝试检测字符串是否具有 NumericValueAlphaValue。如果它只是数字,那么AmountBoolean = true,如果它是数字和Alpha,那么AmountBoolean = false。我找不到让这个工作的方法。似乎我找到了一种方法,但在运行时崩溃,在 logcat 中指出 onClick if 语句是一个问题,但我不知道它为什么或它是如何的。

用于帮助解决此问题的链接是:

How to check if a string contains only digits in Java

Java - See if a string contains any characters in it

Tell if string contains a-z chars

主活动;

public class MainActivity extends AppCompatActivity {

Button Enter;
EditText eName,eDate,eAmount;
ListView lDebtList;
TextView tName,tDate,tAmount;

Boolean AmountBoolean = false;
String currentDate,name,amount,date;
Toast toastName,toastDate,toastAmount;

ArrayList<String> AmountlistItems = new ArrayList<String>();
ArrayList<String> DateListItems = new ArrayList<String>();
ArrayList<String> NameListItems = new ArrayList<String>();

ArrayAdapter<String> AmountAdapter;
ArrayAdapter<String> DateAdapter;
ArrayAdapter<String> NameAdapter;

String numRegex   = ".*[0-9].*";
String alphaRegex = ".*[A-Z].*";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Enter = findViewById(R.id.Enter);
    eAmount = findViewById(R.id.eAmount);
    eDate = findViewById(R.id.eDate);
    eName = findViewById(R.id.eName);
    lDebtList = findViewById(R.id.lDebtList);
    tAmount = findViewById(R.id.tAmount);
    tDate = findViewById(R.id.tDate);
    tName = findViewById(R.id.tName);

    eAmount.clearFocus();
    eDate.clearFocus();
    eName.clearFocus();

    currentDate = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault()).format(new Date());

    tAmount.setText("Amount:");
    tDate.setText("Date Owed");
    tName.setText("Name:");

    eAmount.setHint("$$$");
    eDate.setHint(currentDate);
    eName.setHint("John Doe");


    amount = eAmount.getText().toString();
    date = eDate.getText().toString();
    name = eName.getText().toString();



    if (amount.contains(numRegex) && !amount.contains(alphaRegex)) {
        AmountBoolean = true;
    }
     if(amount.matches(numRegex) && amount.contains(alphaRegex)){
        AmountBoolean = false;
    }


    AmountAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, AmountlistItems);
    lDebtList.setAdapter(AmountAdapter);
}

public void onEnter(View view){
    if(AmountBoolean){
        //AmountAdapter.add(amount);
        AmountAdapter.add(eAmount.getText().toString());
        AmountAdapter.notifyDataSetChanged();
    }

    if(!AmountBoolean){
        toastAmount = Toast.makeText(this, "Please correct Amount Owed", Toast.LENGTH_SHORT);
        toastAmount.show();

    }


}


}  

感谢任何和所有帮助,此外,如果有更简单的方法来实现这一点,请不要犹豫,展示如何。我知道以前有人问过这个问题,但我花了几个小时尝试方法,但没有一个奏效,我正在诉诸于此。

【问题讨论】:

  • 将 amountBoolean 初始化为 false
  • 您在方法之外提供了代码,或者您在另一个方法中提供了方法。请显示minimal reproducible example,而不是从类的其余部分中删除的多行代码
  • 并使用函数,而不是正则表达式。 stackoverflow.com/a/46875081/2308683
  • 完整代码编辑于
  • 模式.*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).*在不区分大小写模式下应该可以工作。

标签: java android regex string character


【解决方案1】:

使用下面的正则表达式:

只检查数字

^[0-9]+$

只检查字母

^[a-zA-Z]+$

检查字符串是否包含字母

.*([a-zA-Z]).*

检查字符串是否为字母数字

^[a-zA-Z0-9]+$

【讨论】:

    【解决方案2】:

    您可以尝试在不区分大小写模式下使用以下模式比较每个字符串:

    .*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).*
    

    代码示例:

    System.out.println("123".matches("(?i:.*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).*)"));
    System.out.println("ABC".matches("(?i:.*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).*)"));
    System.out.println("ABC123".matches("(?i:.*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).*)"));
    

    只有最后一个打印语句输出真。

    Demo

    【讨论】:

    • 鉴于我的逻辑在演示中有效,我敢打赌要么你复制错误,要么你的数据有问题。
    【解决方案3】:

    该函数接受一个字符串并遍历每个字符,以便通过使用 ascii 代码来查找其是字母还是数字,但是它不会区分字母或字母数字,只要出现字母,它就会返回 false,如果不是,它假定所有字符都只是数字我的意思是我认为只有数字和字母没有其他权利,你关心字母数字和数字。第一个范围是小写字母,最后一个是大写字母。你可以随意修改。

    boolean checkString (String st){
        for(int i = 0 ; i < st.size() ; i++){
            char ch = st.charAT(i);
            if((ch>=97 && ch<=122) || (ch>=65 && ch <=90))
                return false;
            }
        }
            return true;
    }
    

    【讨论】:

    • 无论基于代码的答案是否属实,您都需要(至少)提供一些简单的说明,说明为什么它会有效。如果您的假设是错误的,这一点更为重要,此代码只需稍作更改即可正确。谈话很重要。请在删除此答案之前更新答案,因为它是纯代码。
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2012-08-11
    相关资源
    最近更新 更多