0

This is my code;


String KLMN = isekle_dosyaucreti.getText().toString(); if (!KLMN.contains(",")) {

        KLMN = KLMN + ",00";
        Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();

    }
    else if(KLMN.contains(",")){

        String[] data = KLMN.split(",", 2); //before comma
        String[] xab = KLMN.split(",");
        String nn = xab[0];
        String mm = xab[1]; // after comma

        if(mm.length() < 2) {
            KLMN = data[0].concat("," + mm).concat("0");
            Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() == 2) {
            KLMN = data[0].concat(","+xab[1]);
            Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() > 2) {
            Toast.makeText(getApplicationContext(), "The number after the strike cannot be larger than 2 digits", Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() == 0) {
            Toast.makeText(getApplicationContext(), "Should be after the vigrul", Toast.LENGTH_SHORT).show();
        }

    }

<

--------- This is the error: beginning of crash

2020-04-11 23:04:33.560 609-609/com.nicatalibli.bilirkisiasistanti E/AndroidRuntime: FATAL EXCEPTION: main Process: com.nicatalibli.bilirkisiasistanti, PID: 609 java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at com.nicatalibli.bilirkisiasistanti.Activity.BottomActivity.isekle.Deneme(isekle.java:1422) at com.nicatalibli.bilirkisiasistanti.Activity.BottomActivity.isekle.onClick(isekle.java:1462) at android.view.View.performClick(View.java:6597) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Error line;

String mm = xab[1]; >

1 Answers1

0

Length=1 means array have only one element and that is at 0th position, because array starts with 0th position and you are accessing 1st(index=1) position element from array or doing some operation on 1st position. That is what error indicates. in KLMN there is no any digit after "," and that is why there is not xab[1] element which you are trying to access. Now debug your code and check for the real length of the array(tab).

Prashant.J
  • 739
  • 7
  • 12
  • Thanks for answer. I just want to show the error "in KLMN there is no any digit after ","" with Toast. What should I do to do this? – user3286702 Apr 12 '20 at 06:35
  • I want this: The user wrote the sample "125," (did not write numbers after the comma) in the edittex and pressed the "ok button". I want the error message to appear. – user3286702 Apr 12 '20 at 06:47
  • This is again a different question but answer is here, onClickListner() of a button getText() from the EditText and check if no digit after comma then show toast. Toast.makeText(getApplicationContext(),"Your message saying no digit after comma ",Toast.LENGTH_SHORT).show(); – Prashant.J Apr 12 '20 at 06:55
  • I cannot fully understand what you are saying. I've just started. Can you give me an example with a code? – user3286702 Apr 12 '20 at 10:48
  • You need to read about TextWatcher(). You have to implement et.addTextChangedListener() where you will get afterTextChanged() method to override. Inside this method you will see every single input inside the EditText there you can write your logic for checking if there is any digit after ",". I hope this will help and give you some idea. check below post for TextWatcher https://stackoverflow.com/questions/8543449/how-to-use-the-textwatcher-class-in-android – Prashant.J Apr 12 '20 at 11:04