Get an array in JAVA

3

I am new to java, because of this I have the following situation, I would like to get the array in my case 2 of switch , the problem is that it says that 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;
    }   
    
asked by anonymous 20.08.2016 / 21:43

3 answers

2

Java does not know in which order its switch will be executed, as this depends on how the execution will take place. What if it falls into case 2 before falling into case 1 ? There will not be vetTit . The compiler needs to make sure the variable exists.

Declare and initialize your variable in a more external scope, such as as a class field (out of all methods), and the problem will disappear.

    
20.08.2016 / 22:14
1

Declare the variable vetTit [] as class:

public class NomeClasse
{
  protected String vetTit[];

  ...

  protected void metodoQueContemSwitch
  {
    switch(menu) {
        case 1: 
          ... 
          vetTit[]= new String[tam]
          ...
        break;  
       ...
    }
  }

  ...

}
    
20.08.2016 / 22:10
1

Friend makes a book object with attributes (author, publisher ...) after you make a list of book objects. Much easier is not it?

public class livro{
private String autor;
private String editora;

public setAutor(String autor){
this.autor = autor;
}
public getAutor(){
return this.autor;
}

 //para cada atributo autor, editora, ano dois metodos (get e set) 
 //get retorna o valor do atributo do objeto enquanto o set define um valor

}

After making the object, make a list in your main class.

public class usaLivro{

public static void main (String args[]){
List<livro> livros = new ArrayList();

for(int i =0; i<10; i++){
livro l = new livro();
l.setAutor("autor");
//demais atributos
livros.add(livros);
}

//para imprimir os livros use o codigo abaixo

for(int i=0; i<10; i++){
System.out.println("autor livro "+i+": é: "+livros.get(i).getAutor());
}
}
    
25.08.2016 / 20:48