3

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.

Nisheeth Shah
  • 600
  • 1
  • 9
  • 22

3 Answers3

4

Use regular expression below:

To check numeric only

^[0-9]+$

To check alphabets only

^[a-zA-Z]+$

To check if string contains alphabet

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

To check if string is alphanumeric

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

Nisheeth Shah
  • 600
  • 1
  • 9
  • 22
1

You may try comparing each string using the following pattern in case insensitive mode:

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

Code sample:

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]).*)"));

Only the last print statement outputs true.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

the function takes a string and loops through every character in order to find whether its alphabetic or numeric by use of ascii codes however it doesn't differentiate between only alphabetic or alphanumeric whenever a alphabet occurs it returns false, if not it assumes all character were just numbers i mean as i perceive there will be only numbers and alphabets nothing else right and you care about alphanumeric and numeric.The first range is for lower case and last one is for upper case alphabets. You could modify it as u wish.

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;
}
PI_ED
  • 59
  • 8
  • 2
    No matter the truth in a code-based answer, you need to provide (at least) some simple statement about why it will be effective. This is further important in the case that your assumptions were a hair wrong, this code could be correct with a small change. Conversation matter. Please update the answer before this answer is removed for being code-only. – New Alexandria Feb 19 '18 at 17:33