3
I am new in java, because of this I have the following situation, I would like to take the array in my case 2
of switch
, the problem is that it says the value was not initialized, but I first enter the values and size based on what the user entered.
How can I do this?
My code:
switch(menu){
case 1:
System.out.println("Digite quantos livros deseja inserir:");
tam = ler.nextInt();
String vetAutor[] = new String[tam], vetEdit[] = new String[tam],
vetTit[]= new String[tam], vetAss[] = new String[tam];
double vetPreco[] = new double[tam];
for(i=0; i < tam; i++)
{
System.out.println("Digite o titulo: ");
vetTit[i] = ler.next();
System.out.println("Digite o autor: ");
vetAutor[i] = ler.next();
System.out.println("Digite a editora: ");
vetEdit[i] = ler.next();
System.out.println("Digite o assunto: ");
vetAss[i] = ler.next();
System.out.println("Digite o preço: ");
vetPreco[i] = ler.nextDouble();
}
pulaLinha();
tiutloSort(tam, vetTit);
System.out.println("Livros inseridos com sucesso");
tracarLinha();
System.out.println("O que deseja faze agora:");
System.out.println(" 1: Inserir Livros\n 2: Ordenar por título\n 3: "
+ "Ordenar por editor\n 4: Ordenar por preço \n 5: Sair");
break;
case 2:
for(i=0; i < tam; i++)
{
System.out.print(vetTit[i]+"\t");
}
break;
}
I appreciate the explanation, it was of great value!
– Marcos Henrique