I try to reduce a string of characters obtained by means of a QR reader

1

This is the code with which I try to reduce it

    String  valor;

   et1 = (EditText) findViewById(R.id.etCodigoAddProducto);
    String valor=et1.getText().toString();
    et1.setText(valor.substring(20,34));

The logcat tells me that my error is here

             et1.setText(valor.substring(20,34));

Error that m shows logcat

     --------- beginning of crash
      11-23 17:37:17.701 2840-2840/com.example.aceraspire.fujitsu_mysql 
         E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.aceraspire.fujitsu_mysql, PID: 2840
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aceraspire.fujitsu_mysql/com.example.aceraspire.fujitsu_mysql.AddProducto}: java.lang.StringIndexOutOfBoundsException: length=0; index=8
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                 Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=8
                                                                                    at java.lang.String.substring(String.java:1971)
                                                                                    at com.example.aceraspire.fujitsu_mysql.AddProducto.onCreate(AddProducto.java:82)
                                                                                    at android.app.Activity.performCreate(Activity.java:6975)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                    at android.os.Looper.loop(Looper.java:164) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
  11-23 17:37:17.704 1674-2814/? W/ActivityManager:   Force finishing activity com.example.aceraspire.fujitsu_mysql/.AddProducto
 11-23 17:37:17.707 1674-2814/? W/ActivityManager:   Force finishing activity com.example.aceraspire.fujitsu_mysql/.mymenu
11-23 17:37:17.716 1674-1691/? I/ActivityManager: Showing crash dialog for package com.example.aceraspire.fujitsu_mysql u0
11-23 17:37:17.725 1403-2017/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
11-23 17:37:17.726 1674-4291/? D/OpenGLRenderer: HWUI GL Pipeline
11-23 17:37:17.758 1394-1394/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 2654208
    
asked by Sofia 23.11.2017 в 23:24
source

3 answers

0

Sofia, the value within the variable valor may not have the length you are trying to obtain, in this case 34, if you do not have it you would get an IndexOutOfBoundsException,

as shown in the LogCat:

  

Caused by: java.lang.StringIndexOutOfBoundsException: length = 0;   index = 8

I recommend you validate in this way to avoid the error:

String valor = et1.getText().toString();

if(valor.isEmpty() && valor.length()>=34){
       //Reduce la cadena.
     valor = et1.setText(valor.substring(20,34));           
  }else{
       //la longitud no es mayor o igual a 34, no podrá reducir la cadena.
  }
    
answered by 23.11.2017 / 23:48
source
0
  String  valor; // Tienes una variable global aquí

   et1 = (EditText) findViewById(R.id.etCodigoAddProducto);

You declare a value variable here again, which is the method variable,

   String valor=et1.getText().toString();
    et1.setText(valor.substring(20,34));

You are probably taking the "value" of the global variable that is empty.

  et1.setText(valor.substring(20,34));

In any case, make sure that EditText (et1) has data.

    
answered by 24.11.2017 в 01:05
0

The correct value is not being captured and / or the value does not have 34 digits .. this is according to the condition that you put in order to reduce the size of the same value.

 et1.setText(valor.substring(20,34));

Remember that substring works in the following way. (using your data)

of all the text (value) only a portion is obtained from the 20 digit up to the digit 34

where yes or if the text (value) must have at least 34 digits.

You can make an if condition to be able to validate if the text (value) has 43 digits or more ..

String value = et1.getText (). toString ();

if(valor.isEmpty() && valor.length()>=34){
     valor = et1.setText(valor.substring(20,34));           
  }else{
       //el texto (valor) no cumple la condición, el texto tiene menos de 34 dígitos
  }
    
answered by 24.11.2017 в 01:24