1. Are you ready for the Galaxy S20? Here is everything we know so far!

catching and stoping a one decimal point error ?

Discussion in 'Android Development' started by androidkeep, Aug 9, 2012.

  1. androidkeep

    androidkeep Lurker
    Thread Starter

    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 (Text):
    1.  
    2.     <EditText
    3.         android:id="@+id/edittext01"
    4.         android:layout_width="fill_parent"
    5.         android:layout_height="wrap_content"
    6.         android:inputType="number|numberDecimal"
    7.         android:lines="1" >
    8.     </EditText>
    9.  
    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 (Text):
    1.  
    2. if (stop == 0) // checking for one decimal only and nothing else
    3. {
    4.  String errx =null;
    5.  errx = (edtxt01.getText().toString());
    6.  Log.i("ONE DEC:", "START OF IF xxxx: " + errx);
    7.  if (edtxt01.length() == 1 ) // *********** 1st if
    8.  {
    9.   Log.i("ONE DEC:", "inside OF IF xxxx: starting err if" + errx);
    10.   if (errx == ".") // ********** 2nd if
    11.   {
    12.    Log.i("***ONE DEC:", "inside OF errx if" + errx);
    13.   CharSequence text1 = "error message goes here";
    14.   Toast toast1 = Toast.makeText(context, text1, duration);
    15.   toast1.show();
    16.   txtv4.setText(null);
    17.   stop = 1;
    18.   }
    19.  };
    20. };
    21.  
    22.  
    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
     



    1. Download the Forums for Android™ app!


      Download

       
  2. androidkeep

    androidkeep Lurker
    Thread Starter

    .contains() works

    this works:

    Code (Text):
    1.    errx = "";
    2.    errx = (edtxt01.getText().toString());
    3.    Log.i("ONE DEC:", "START OF IF xxxx: " + errx);
    4.    if ((edtxt01.length() == 1 )&&(errx.contains(".")))
    5.  
     
Loading...

Share This Page

Loading...