Reputation: 1364
In my edittext I need to check whether it is entered zero only. For example I have an edittext for entering the IFSc code for Bank. It accepts only letters and digits. So if suppose user is entering zero only for 14 characters. I need to check, One solution I found is Covert the string into integer and add together, whether it is zero means user entered zer only for that I wrote code, but it wont working. Can anybody help me from this problem.
int ifno = Integer.parseInt(ifsc);
int sum = 0;
while (ifno == 0)
{
sum = sum + ifno % 10;
ifno = ifno / 10;
}
if(sum == 0)
{
Toast.makeText(Neft_Details.this, "Not a valid IFSC Code", Toast.LENGTH_LONG).show();
}
Upvotes: 1
Views: 133
Reputation: 11
Well another way to do this is, you can try to define a test function like this in your code.
public static boolean isAllZeros(String text) {
char zero = '0';
for (int i=0; i<text.length()-1; i++) {
if (!(text.charAt(i) == zero)) {
return false;
}
}
return true;
}
Then test your string entered in the EditText by passing to this function. This way you can avoid parsing it into an integer and you can directly check the entered string.
if(Test.isAllzeros(editextString)) {
Toast.makeText(Neft_Details.this, "Not a valid IFSC Code",
Toast.LENGTH_LONG).show();
} else {
// Do whatever you want in here.
}
Upvotes: 0
Reputation: 3906
I think you need to change your while
loop try this..
String ifsc = "00000000000000";
int ifno = Integer.parseInt(ifsc);
int sum = 0;
while (ifno != 0){
ifno = ifno / 10;
sum = sum + ifno;
}
if(sum == 0){
Toast.makeText(Neft_Details.this, "Not a valid IFSC Code", Toast.LENGTH_LONG).show();
// System.out.println("Invalid value");
}
Upvotes: 1
Reputation: 6973
Try to use valueOf() method of Integer. Following is the example to findout the exact value of the given string.
String value = "000000";
value = Integer.valueOf(value).toString();
System.out.println("result for check: "+value);
Output:
0
Hope this will help for you
Upvotes: 0
Reputation: 23
i make a very simple example for you and it works:
EditText info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
info = (EditText)findViewById(R.id.infoNum);
Button okButton = (Button)findViewById(R.id.okBtn);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int infoINt = Integer.parseInt(info.getText().toString());
if(infoINt == 0){
Toast.makeText(getBaseContext(),"Not a valid IFSC Code",Toast.LENGTH_LONG).show();
}else{
// do your true logic here
}
}
});
}
Maybe error was in your logic area .
using ValidationExpression also works fine
Upvotes: 0