Extracting Numbers from a String

Asked

Viewed 860 times

5

I have a string in the following format:

01 - 02 - 03 - 04 - 45 - 86

I need to put these numbers in one array int[6]. What is the best way to do this operation?

3 answers

10

You can do it like this:

public class Test {
    public static void main(String[] args) {
        String s = "01 - 02 - 03 - 04 - 45 - 86";
        String[] sp = s.split(" - ");
        int n[] = new int[sp.length];

        for (int i = 0; i < sp.length; i++) {
            n[i] = Integer.parseInt(sp[i]);
        }
        for (int i = 0; i < sp.length; i++) {
            System.out.println(" " + n[i]);
        }
    }
}

You can see the documentation here

  • Thank you Leornardo worked perfectly.

4

An alternative is to use regular expressions. Therefore, no matter how the numbers are in your String, regex will extract them for you. See below for the implementation:

String numeros = "05abc474 - 651ssss1120;lks01=9";
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(numeros);

StringBuilder nroExtraidos = new StringBuilder();
while (m.find()) {
    nroExtraidos.append(m.group().trim() + "/");
}

System.out.println(nroExtraidos);

Exit:

05/474/651/1120/01/9/

Note that you can have a completely varied string between characters and numbers. Still you can extract the numbers.

0

Hi. I did an Object Orientation proof that the objective of the exercise was to receive a string of type "1 + 2 * 4" and to separate what was signal and what was number and present the result of the operation.

In the proof I solved using List and with bubble method to make stack effect in the precedence cases (multiplication and division).

My calculator class looked like this:

class Calculator {

private LinkedList<Character> numeros = new LinkedList<Character>();
private LinkedList<Character> op = new LinkedList<Character>();

Calculadora(String formula) {

    LinkedList<Character> valores = new LinkedList<Character>();

    valores.add('1');
    valores.add('2');
    valores.add('3');
    valores.add('4');
    valores.add('5');
    valores.add('6');
    valores.add('7');
    valores.add('8');
    valores.add('9');
    valores.add('0');

    LinkedList opValidos = new LinkedList();
    opValidos.add('+');
    opValidos.add('-');
    opValidos.add('*');
    opValidos.add('/');

    for (int i = 0; i < formula.length(); i++) {

        if (valores.contains(formula.charAt(i))) {

            numeros.add(formula.charAt(i));

        } else if (opValidos.contains(formula.charAt(i))) {

            op.add(formula.charAt(i));

        }

    }

}

public double resolve() {

    if (op.contains('*') || op.contains('/')) {

        arrumaExpressao();

    }

    int resultado = Character.getNumericValue(numeros.get(0));

    for (int i = 0; i < op.size(); i++) {

        if (op.get(i) == '+') {

            resultado += Character.getNumericValue(numeros.get(i + 1));

        } else if (op.get(i) == '-') {

            resultado -= Character.getNumericValue(numeros.get(i + 1));

        } else if (op.get(i) == '*') {

            resultado *= Character.getNumericValue(numeros.get(i + 1));

        } else if (op.get(i) == '/') {

            resultado /= Character.getNumericValue(numeros.get(i + 1));

        }

    }

    return resultado;
}

public void arrumaExpressao() {

    for (Character op1 : op) {

        if (op.contains('*')) {
            int i = op.indexOf('*');

            Collections.swap(op, 0, i);
            Collections.swap(numeros, 0, i);
            Collections.swap(numeros, 1, i + 1);
        }

        if (op.contains('/')) {
            int i = op.indexOf('/');

            Collections.swap(op, 0, i);
            Collections.swap(numeros, 0, i);
            Collections.swap(numeros, 1, i + 1);
        }
    }
}

If you want to take a look at junit and class, you can find it on my github as well as give suggestions.

https://github.com/GustavoEmmel/Aulas

Browser other questions tagged

You are not signed in. Login or sign up in order to post.