0

I got an ArrayList "ArrayList1" with content like

0090113itemone ,
1000120itemtwo ,
0090113itemthree

There are always 7 numbers in front of an item .

Now I want to remove the numbers to assign only the items to a ListView. I tried to convert the Array to String with

String[] arr = ArrayList1.toArray(new String[ArrayList1.size()]);

and then use substring to cut the first 7 positions from the string

result.append(arr.substring(0,7));

But this will get me an error Cannot invoke substring(int, int) on the array type String[]

So I would need a way to get rid of these numbers. Either how to solve this error or, if you got a better idea, I'm also open to that.

1
  • result.append(arr.substring(0,7)); substring(int,int) over an array? Didn't it through any compilation error? You should iterate through the string array and do a substring. Commented Dec 20, 2013 at 13:22

7 Answers 7

1

you have to iterate over the array and do it for every string, because substring() is a method of the string class and not of the array class. The errormessage Cannot invoke substring(int, int) on the array type String[] tell you that you try do build a substring of an Stringarray.

change:

result.append(arr.substring(0,7));

to:

result.append(arr[0].substring(0,7));

and put in in a for-loop:

for(int i = 0; i<arr.length(); i++)
    result.append(arr[i].substring(0,7));
Sign up to request clarification or add additional context in comments.

3 Comments

Now I get an error when I try to put it in the ListView on: 'ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result); lv.setAdapter(arrayAdapter);' The constructor ArrayAdapter<String>(MainActivity, int, StringBuilder) is undefined
because you try to put the stringbuilder into an string list. convert the stringbuilder to an string with StringBuilder.toString()
Only change the error to The constructor ArrayAdapter<String>(MainActivity, int, String) is undefined
0

The error "Cannot invoke substring(int, int) on the array type String[]" means that the method does not work on this type of object.

It works on a String though.

You don't need to create an Array out of your ArrayList like this:

String[] arr = ArrayList1.toArray(new String[ArrayList1.size()]);

Instead, use your ArrayList right away. I highly recommend renaming it to Java Camel Case: "arrayList1" to avoid confusion with classes. Use Regex to replace numbers in a String. Try running this for-loop:

for (String item : arrayList1) {
    String result = item.replaceAll("[0-9]","");
    System.out.println(result);
}

Comments

0
for (int i=0;i<arr.length;i++) {
   arr[i] = arr[i].substring(7);
}

Comments

0

Do like this

for (String s:arr) {
    result.append(s.replaceAll("\\d",""));
}

Comments

0

You have to edit each element before copying it. I'd recoment the following:

List<String> newList = new ArrayList<>();    
for (String element : arrayList1)
{
  String elementWithoutNumber = element.subString(7);
  newList.add(elementWithoutNumber);
}
String[] arr = ArrayList1.toArray(new String[newList.size()]);

Comments

0

Try this code :

for (String s:arr) {
    result.append(s.substring(7,s.length()));
}

Comments

0

Loop through your ArrayList to start with.
Then at least two options you have:

1) Use the String.replace method.

String x = str.replaceAll("\\d", ""); 

Call this method on each string from your ArrayList.
Then handle the x which you get as you need.

2) But if your strings may contain digits after their
position 7 and you want those digits included, then loop
through the strings and do:

String x = str.substring(7);

Then do what you need with x.

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.