检查String是否包含数值和Alpha值

时间:2018-02-19 05:16:38

标签: java android regex string character

我正在尝试检测字符串是否具有 NumericValue AlphaValue 。如果它只是数字然后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

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();

    }


}


}  

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

3 个答案:

答案 0 :(得分:3)

使用下面的正则表达式:

仅检查数字

^[0-9]+$

仅检查字母

^[a-zA-Z]+$

检查字符串是否包含字母

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

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

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

答案 1 :(得分:0)

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

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

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

Demo

答案 2 :(得分: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;
}
相关问题