Grab a JAVA array

Asked

Viewed 580 times

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;
    }   

3 answers

2


Java does not know what order you are in switch will be executed, since it depends on how the execution will take place. And if you fall into the case 2 before falling into the case 1? Will not exist vetTit. The compiler needs to make sure that the variable exists.

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

  • I appreciate the explanation, it was of great value!

1

Friend makes a book object with attributes ( author, publisher ... ) after vc makes a book object list. Much easier not?

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 its 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());
}
}
  • Ow guy thanks for the feedback, but how would I do it? I’m kind of new at OO

  • 1

    edited the answer I hope to have helped

  • thank you very much man, it was of great value, sanou until some doubts that I had of OO

1

Declare the variable vetTit[] as the class:

public class NomeClasse
{
  protected String vetTit[];

  ...

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

  ...

}
  • I appreciate the exemplification, was of great value to the real understanding!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.