• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps catching and stoping a one decimal point error ?

Alright how does one catch and stop a one decimal point error ?
The user inputs a single . and that is all; you can not allow that because you will be doing math with that input.if you have this in your xml file:
Code:
    <EditText
        android:id="@+id/edittext01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number|numberDecimal"
        android:lines="1" >
    </EditText>

and the user types just one decimal and nothing else the program will blow up.

I tried the following code but the program blows up at the second IF.
At first the two IFs were just one line, they got broken up to see where the problem was

As you can see if the length of the user input is only 1 we check to see if it is only one decimal only

The program blows up at the second IF, but errx is a STRING.
I used the Log.i to see how far to program went. And the errx is a "."

Code:
if (stop == 0) // checking for one decimal only and nothing else 
{
 String errx =null;
 errx = (edtxt01.getText().toString());
 Log.i("ONE DEC:", "START OF IF xxxx: " + errx);
 if (edtxt01.length() == 1 ) // *********** 1st if
 {
  Log.i("ONE DEC:", "inside OF IF xxxx: starting err if" + errx);
  if (errx == ".") // ********** 2nd if
  {
   Log.i("***ONE DEC:", "inside OF errx if" + errx);
  CharSequence text1 = "error message goes here";
  Toast toast1 = Toast.makeText(context, text1, duration);
  toast1.show();
  txtv4.setText(null);
  stop = 1;
  }
 };
};

So how does one catch and stop a one decimal point only error ?

Here are some of the error messages:

thread exiting with uncaught exception (group=0x4001b188)

Uncaught handler: thread main exiting due to uncaught exception

AndroidRuntime(776): Caused by: java.lang.reflect.InvocationTargetException

AndroidRuntime(776): Caused by: java.lang.NumberFormatException
 
.contains() works

this works:

Code:
   errx = "";
   errx = (edtxt01.getText().toString());
   Log.i("ONE DEC:", "START OF IF xxxx: " + errx);
   if ((edtxt01.length() == 1 )&&(errx.contains(".")))
 
Back
Top Bottom