-1
Well, I have an array, I add values to that array, I send them to a method in another class that puts the values of that array into one string only and I add that single string into an Arraylist, but when I show it, it only shows several "null".
main class:
class Principal {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
Logica chamar = new Logica(valores);
String[][] valores = new String[3][2];
ArrayList colecao = new ArrayList();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (j == 0) {
System.out.println("Insira um nome.");
} else {
System.out.println("Insira um número.");
} valores[i][j] = entrada.next();
}
} colecao.add(chamar);
for (Object resolucao:colecao) {
System.out.println(resolucao.toString());
}
}
}
logic class:
class Logica {
String valores;
public Logica(String[][] valores) {
valores = new String[3][2];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
this.valores = valores[i][j] + "\n";
}
}
}
public String toString() {
return valores;
}
}
What is wrong?
I made the changes, it worked as expected, however, at the time of displaying, a "null" appears in the first line before the entered data. It was "nullvalor", the rest is normal. Where could this null be emerging?
– ptkato
Ah, I think that’s why the global variable
valores
starts as nil.– ptkato
@Patrick I think it’s because in class
Logica
failed to declare the value of the attributevalores
thus:String valores = "";
– Piovezan