I have the following code that gets an element from the terminal with the Scanner class and adds it to a shopping cart array. It works fine, except that when I try to print the cart it'll show the unfilled positions of the array as "null". I digged over some topics here and someone sugested to create the first array with blank spaces for each of the 5 position (like String[] test = new String[]{"","","","",""};, but that didn't work. What should I do to "fix" this?
import java.util.Arrays;
import java.util.Scanner;
public class ex03 {
public static void main(String[] args) {
Scanner frutas = new Scanner(System.in);
String[] nomesFrutas = new String[5];
System.out.println("Insira a sua lista de compras.");
for (int i = 0; i <= nomesFrutas.length; i++) {
nomesFrutas[i] = frutas.next();
System.out.println("As frutas no seu carrinho são: \r\n " + Arrays.toString(nomesFrutas));
}
}
}