1

Hey i was trying to recognize numbers from a string and convert them into a String array, but with this code what i get is:

IE: [null1, null2, exc..]

I dont know how to take out this null value.
I also would like to find a way to do this, without an array, because i should increase its length manually.

Any tips? Thanks!

public class ProvaToArray {
    public static void main(String[] x) {
        String s = "1-2-3-4-lorem23ip567um1";
        String[] ris = toArray(s);
        System.out.println(Arrays.toString(ris)); //should printout  [1, 2, 3, 4, 23, 567, 1]
    }

    public static String[] toArray(String s){
        String[] l = new String[10];
        int count = 0;
        for(int i = 0; i < s.length() - 1; i++){
            if(estNumber(s.charAt(i) + "")){
                l[count] += "" + s.charAt(i);
                if(!estNumber(s.charAt(i+1) + "")){
                    count++;
                }
            }
            if(i+1 == s.length()-1){
                if(estNumber(s.charAt(i+1) + "")){
                    l[count] = "" + s.charAt(i+1);
                }
            }
        }
        return l;
    }

    public static boolean estNumber(String i){
        if(i.matches("^-?\\d+$"))
            return true;
        else
            return false;
    }
}

1 Answer 1

4

When you initialize your String array :

String[] l = new String[10];

Each index contains null.

When you append a String to that null, you get the String "null" append to your String.

If you change :

l[count] += "" + s.charAt(i);

to

if (l[count] == null)
    l[count] = "" + s.charAt(i);
else
    l[count] += "" + s.charAt(i);

You'll get rid of the "null" String.

I saw the code you posted as an answer. It shouldn't be posted as an answer if it doesn't answer the question. You should have added it to the question.

Anyway, your problem is accessing the i+2'th character of the String s, which leads to IndexOutOfBoundsException in the final iteration of your loop. Instead of checking the i'th, i+1'th and i+2'th character in the same iteration, you should check one character in each iteration, and use a local variable to hold the current number.

Your code can be much simpler than it currently is :

public static String[] toArray(String s)
{
    ArrayList<String> m = new ArrayList<>();
    StringBuilder currentNumber = new StringBuilder();
    for(int i = 0; i < s.length(); i++) {
        if(Character.isDigit(s.charAt(i))) {
            currentNumber.append (s.charAt(i));
        } else {
            if (currentNumber.length() > 0) {
                m.add(currentNumber.toString());
                currentNumber.setLength(0);
            }
        }
    }
    if (currentNumber.length() > 0) {
        m.add(currentNumber.toString());
    }                    
    return m.toArray(new String[m.size()]);
}

This produces the array [1, 2, 3, 4, 23, 567, 1] for the input you used.

Sign up to request clarification or add additional context in comments.

9 Comments

Awesome thanks! But if i dont want to use a String[], but an arrayList? how can i get rid of this?
@Benny If you use an ArrayList, you must either call list.add("" + s.charAt(i)) or list.set(count,list.get(count) + s.charAt(i)); depending on whether or not you already added an element to the count index of the List. You'll still need a condition.
My real problem is to add it on the same index in the arrayList.. i tried with set, add, but still cant manage it
@Benny list.set(count,list.get(count) + s.charAt(i)); should work, as long as list.size()>count.
it throws IndexOutOfBoundExceptions. Dont understand why
|

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.