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.

8
  • 2
    initialize amountBoolean to false initially Commented Feb 19, 2018 at 5:17
  • You have given code outside of a method, or you have a method inside another. Please show a minimal reproducible example, not multiple lines of code that are removed from the rest of the class Commented Feb 19, 2018 at 5:21
  • And use a function, not regexes. stackoverflow.com/a/46875081/2308683 Commented Feb 19, 2018 at 5:23
  • Full code was edited in Commented Feb 19, 2018 at 5:26
  • The pattern .*(?:[A-Z].*[0-9]|[0-9].*[A-Z]).* in case insensitive mode should work. Commented Feb 19, 2018 at 5:28

3 Answers 3

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]+$

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Using this i set AmountBoolean to false if this is true, but AmountBoolean comes out false no matter what
Given that my logic is working in the demo, I'd wager that either you copied wrongly, or your data has a problem.
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;
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.