I am trying to detect if a string has NumericValue and AlphaValue. If it is just Numeric then AmountBoolean = true
and if it is Numeric and Alpha then AmountBoolean = false
. I cant find a way to get this to work. It seems I have found a way but crashes during runtime stating in the logcat that the onClick
if statement is an issue but i dont know why it is or how it is.
links used to help with this are:
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();
}
}
}
Any and all help is appreciated, Also if there is a simpler way of achieving this, don't hesitate to show how. I know this question has been asked before but ive spent hours trying methods out but none have worked and am resorting to this.