I have a String in the following format:
01 - 02 - 03 - 04 - 45 - 86
I need to put these numbers in array int[6]
. What is the best way to do this?
I have a String in the following format:
01 - 02 - 03 - 04 - 45 - 86
I need to put these numbers in array int[6]
. What is the best way to do this?
You can do 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 view the here documentation
An alternative is to use regular expressions . So, no matter how the numbers in your String are, with regex will draw 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);
Output:
05/474/651/1120/01/9/
See that you can have a String completely varied between characters and numbers. You'll still be able to extract the numbers.
Hi. I did an Object Orientation test that the purpose of the exercise was to receive a string of type "1 + 2 * 4" and separate what was sign and what was number and present the result of the operation.
In the test I solved using List and bubble method to do stack effect in cases of precedence (multiplication and division).
My calculator class looks 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 the junit and the class, you can find it in my github as well as give suggestions.