Java registration creation

I am creating an academic system in Java, but I have a problem.

I have 4 classes (Principal, Pessoa, Aluno and Professor). I believe it is something simple, the error that appears is in class Principal

error: cannot find symbol - symbol: variable aluno - location: class Principal

I believe I have to connect the classes, but I don't know how to do that.

Class Pessoa:

package Sistema2;

public class Pessoa {

    private String nome;
    private int idade;
    private char genero;

    public Pessoa(String nome, int idade, char genero) {
        this.nome = nome;
        this.idade = idade;
        this.genero = genero;
    }

    @Override
    public String toString() {
        return "Pessoa{" + "nome=" + nome + ", idade=" + idade + ", genero=" + genero + "}';
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

Class Aluno:

package Sistema2;

public class Aluno extends Pessoa {

    private String ra;

    public Aluno(String nome, int idade, char genero) {
        super(nome, idade, genero);
    }

    public String getRa() {
        return ra;
    }

    public void setRa(String ra) {
        this.ra = ra;
    }

Class Principal:

package Sistema2;

import java.util.Scanner;
import java.util.ArrayList;

public class Principal {
    public static void main (String[] args){

        Scanner scan = new Scanner(System.in);
        ArrayList<Aluno> listaDeAlunos = new ArrayList<>();

        int op = 0;

        do{
            System.out.println("##ESCOLHA UMA OPÇÃO##\n");
            System.out.println("1 - Cadastrar Aluno");
            System.out.println("2 - Cadastrar Professor");
            System.out.println("3 - Sair \n");
            System.out.println("Digite uma opção: ");
            op = scan.nextInt();

            switch(op){
                case 1:
                    System.out.println("Bem vindo ao sistema de cadastro de Alunos\n");

                    System.out.println("Nome: ");
                    aluno.setNome(scan.nextLine()); //Ocorre erro

                    System.out.println("Idade: ");
                    aluno.setIdade(scan.nextInt()); //Ocorre erro

                    System.out.println("Gênero ('F' para Feminino e 'M' para Masculino): ");
                    aluno.setGenero(scan.next().charAt(0)); //Ocorre erro
                    scan.nextLine();

                    System.out.println("RA: ");
                    aluno.setRa(scan.nextLine()); //Ocorre erro

                case 2:
                    System.out.println("Bem vindao ao sistema de cadastro de Professores");

                case 3:
                    break;

                default:
                    System.out.println("Opção inválida, tente novamente.");
            }
        }while(op != 3);
    }
}
Author: Victor Stafusa, 2016-08-17

1 answers

Your main class:

package Sistema2;

import java.util.Scanner;
import java.util.ArrayList;

public class Principal {
    public static void main (String[] args){

        Scanner scan = new Scanner(System.in);
        ArrayList<Aluno> listaDeAlunos = new ArrayList<>();

        int op = 0;

        do{
            System.out.println("##ESCOLHA UMA OPÇÃO##\n");
            System.out.println("1 - Cadastrar Aluno");
            System.out.println("2 - Cadastrar Professor");
            System.out.println("3 - Sair \n");
            System.out.println("Digite uma opção: ");
            op = scan.nextInt();

            switch(op){
                case 1:
                    System.out.println("Bem vindo ao sistema de cadastro de Alunos\n");

                    System.out.println("Nome: ");
                    aluno.setNome(scan.nextLine()); //Ocorre erro

                    System.out.println("Idade: ");
                    aluno.setIdade(scan.nextInt()); //Ocorre erro

                    System.out.println("Gênero ('F' para Feminino e 'M' para Masculino): ");
                    aluno.setGenero(scan.next().charAt(0)); //Ocorre erro
                    scan.nextLine();

                    System.out.println("RA: ");
                    aluno.setRa(scan.nextLine()); //Ocorre erro

                case 2:
                    System.out.println("Bem vindao ao sistema de cadastro de Professores");

                case 3:
                    break;

                default:
                    System.out.println("Opção inválida, tente novamente.");
            }
        }while(op != 3);
    }
}

Could you point in which row of class Principal Did you declare the variable aluno? Yeah, not me either! And the compiler agrees with me.

You didn't declare the variable aluno anywhere and that's why the compiler complains.

There are at least three possible solutions. Pick one.

Solution 1

Change the constructor from Pessoa to:

public Pessoa() {
}

Change the constructor from Aluno for:

public Aluno() {
}

Add this right after the line of case 1:

Aluno aluno = new Aluno();

Solution 2

Change the contents of your case 1: for this:

            case 1:
                System.out.println("Bem vindo ao sistema de cadastro de Alunos\n");

                System.out.println("Nome: ");
                String nome = scan.nextLine();

                System.out.println("Idade: ");
                int idade = scan.nextInt();

                System.out.println("Gênero ('F' para Feminino e 'M' para Masculino): ");
                char genero = scan.next().charAt(0);
                scan.nextLine();

                System.out.println("RA: ");
                Aluno aluno = new Aluno(nome, idade, genero);
                aluno.setRa(scan.nextLine());

Solution 3

Looks like solution 2, but you change the constructor from Aluno to this:

public Aluno(String nome, int idade, char genero, String ra) {
    super(nome, idade, genero);
    this.ra = ra;
}

And the end of case 1: looks like this:

                System.out.println("RA: ");
                String ra = scan.nextLine();
                Aluno aluno = new Aluno(nome, idade, genero, ra);
 3
Author: Victor Stafusa, 2016-08-17 19:26:02