检查字符串是否包含数字值和字母值

3
我正在尝试检测一个字符串是否具有数字值字母值。如果只有数字,则AmountBoolean = true,如果既有数字又有字母,则AmountBoolean = false。我找不到一种方法使其正常工作。看起来我已经找到了一种方法,但是在运行时崩溃,并在logcat中声明onClick if语句是一个问题,但我不知道为什么或如何解决它。
用于帮助的链接如下: 如何检查Java中的字符串是否仅包含数字 Java - 查看字符串中是否包含任何字符 判断字符串中是否包含a-z字符 MainActivity:
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();

    }


}


}  

非常感谢您的帮助。如果有更简单的方法,请不要犹豫,告诉我。我知道这个问题以前已经被问过了,但我已经花了几个小时尝试各种方法,但都没有成功,所以只好求助于此。


2
最初将amountBoolean初始化为false。 - TheLostMind
您已经在方法外给出了代码,或者您有一个方法在另一个方法内部。请展示一个最小完整可运行示例([MCVE]),而不是多行与类的其余部分隔离开的代码。 - OneCricketeer
使用函数而不是正则表达式。https://dev59.com/V2sy5IYBdhLWcg3w4yDc#46875081 - OneCricketeer
@TimBiegeleisen 我该如何实现它? - MickeyT1997
遗憾的是,AmountBoolean 始终为 false。 - MickeyT1997
显示剩余3条评论
3个回答

4

使用以下正则表达式:

检查数字

^[0-9]+$

检查字母

^[a-zA-Z]+$

检查字符串是否包含字母

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

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

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


1

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

.*(?:[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]).*)"));

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

演示


使用这个代码,如果“this”为真,我将AmountBoolean设置为false,但无论如何AmountBoolean都是false。 - MickeyT1997
鉴于我的逻辑在演示中是有效的,我敢打赌,要么是你复制错误了,要么是你的数据有问题。 - Tim Biegeleisen

0

该函数接受一个字符串并循环遍历每个字符,以便通过使用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;
}

2
无论代码答案的真实性如何,您都需要提供(至少)一些简单的陈述,说明为什么它会有效。在您的假设有点错误的情况下,这更加重要,因为通过小的更改,这段代码可能是正确的。交流很重要。在此答案因仅包含代码而被删除之前,请更新答案。 - New Alexandria

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接