0

i've problem when adding string to a stringArray. It crashes every time:

ArrayList<String> newString, limits;

    String pruebas;     
    pruebas=e1.getText().toString();
    if (pruebas == null || pruebas.equals("")) {
        Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
        return;
    }
    else{
        limits.add(pruebas);
    }

Any help would be appreciated!

Thankyou

2
  • 2
    Is this string-array or Arraylist<String>? Commented Jan 11, 2013 at 11:35
  • And please post logcat output Commented Jan 11, 2013 at 11:36

2 Answers 2

5

Please Use below code, it will solve your problem.

Code for Add String into ArrayList

ArrayList<String> limits = new ArrayList<String>();

String pruebas=e1.getText().toString();

if (pruebas == null || pruebas.equals("")) {
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
    return;
}else {
    limits.add(pruebas);
}

Code for Add String into String Array, Here 10 is size of String Array and index is position of string array which you want to store the string value.

String[] limits = new String[10];

String pruebas=e1.getText().toString();

if (pruebas == null || pruebas.equals("")) {
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
    return;
}else {
    limits[index] = pruebas;
}
Sign up to request clarification or add additional context in comments.

1 Comment

How do you view an indexed limits? For example, limits[0]. I tried that it gives me: The type of the expression must be an array type but it resolved to ArrayList<String>
2

You have not intialize ArrayList... so use this below

   ArrayList<String> newString, limits;

   limits = new ArrayList<String>();

   newString = new ArrayList<String>();

    String pruebas;     
    pruebas=e1.getText().toString();
    if (pruebas == null || pruebas.equals("")) {
        Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
        return;
    }
    else{
        limits.add(pruebas);
    }

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.