0

How can I get the value of EditText then store it in my array?

This is my full code:

EditText op = (EditText)v.findViewById(R.id.operator);
array=new int [Integer.valueOf(op.getText().toString())];

for (i = 0;i<(array.length+1);i++){
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    final View v2=inflater.inflate(R.layout.inputangka, null) ;
    new AlertDialog.Builder(MainActivity.this)
        .setIcon(R.drawable.ic_launcher)
        .setTitle("number of-"+(i-1)+":")
        .setView(v2)
        .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dlg, int sumthin) {
                EditText number = (EditText)v2.findViewById(R.id.number);
                int number1 = Integer.valueOf(number.getText().toString());
                array [i]= number1; //error line

My error log:

02-05 16:33:31.374: E/AndroidRuntime(431): java.lang.ArrayIndexOutOfBoundsException

How do I solve this?

2
  • 4
    for (i = 0;i<(array.length+1);i++) - Seems like that's your problem. The +1 puts you beyond the last index in the array. Commented Feb 5, 2013 at 17:05
  • i have remove +1 and i am compile again but my error same,,,,on array [i]= number1; Commented Feb 5, 2013 at 17:09

4 Answers 4

1

This occur because for ex you enter 5 in edit text. Now you are iterate the loop until less than 6 from initial value 0.

When counter reach to 5 then your condition will become true 5 < 6 then but there array size is 5 here you are accessing the 6th element. So it throwing the ArrayIindexOutOfBound Exception.

To get rid from this problem.

Try this.

for (i = 0; i<(array.length);i++){
    //Code
}
Sign up to request clarification or add additional context in comments.

6 Comments

The second one will not actually cause an exception. It would just set the title to "number of--1:", "number of-0:", and so on.
i have removed then my problem is same,,,,,
@user1759316 Same error then post the logcat i can not imagine
FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException at com.example.dialog_looping.MainActivity$1$1.onClick(MainActivity.java:48) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507)
@user1759316 What is written on MainActivity.java:48 and have you made change in java file what i have told above and make sure you have compiled the java class.
|
0

remove +1 from for (i = 0;i<(array.length+1);i++){ and it will work like a charm.

6 Comments

same error, this is my error logcat and my full code docs.google.com/document/d/…
yeah, sorry i am newbie, please give me code for looping dialog,,,
I will give it, But what is the logic, I mean what you are trying to do?
i want to create dialog loop, if i am input 3 then dialog looping 3x, and my value on dialog loop store in array then show. if on dialog looping 1st iam input 10 then dialog show 10, 2nd dialog i am input 20 then dialog show 10+20,i am input 3rd 30 then dialog show 10+20+30 and etc depending on the input number of looping dialog
please wait i am upload my design
|
0

you just need to replace the for condition by following code.

for (i = 0;i<(array.length-1);i++){

It will work for sure.

Comments

0

See, your onClick() is getting executed after your loop is finished executing and hence the value of i is one more than the array last index. For a workaround, save that value into another temporary integer variable and then send it. It will surely work then.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.