Java creation of Register

Asked

Viewed 2,329 times

1

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 the 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 this.

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

1 answer

3

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 to which class line Principal you declared the variable aluno? Yeah, me neither! And the compiler agrees with me.

You have not declared the variable aluno nowhere and why the compiler complains.

There are at least three possible solutions. Choose a.

Solution 1

Change the constructor of Pessoa for:

public Pessoa() {
}

Change the constructor of Aluno for:

public Aluno() {
}

Add this right after the line case 1:

Aluno aluno = new Aluno();

Solution 2

Change the content of your case 1: for that reason:

            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

Similar to solution 2, but you change the constructor of Aluno for that reason:

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

And the end of case 1: gets like this:

                System.out.println("RA: ");
                String ra = scan.nextLine();
                Aluno aluno = new Aluno(nome, idade, genero, ra);
  • I used solution 3.. worked.. however when placing run appears only the name and age and then error Exception in thread "main" java.util.Inputmismatchexception at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) At system2.Principal.main(Home.java:30) C: Users 474797 Appdata Local Netbeans Cache 8.1 executor-snippets run.xml:53: Java returned: 1 BUILD FAILURE (total time: 8 seconds)

  • @Tejota This happens when you type for age something that is not a number. It can also be an annoying problem that I’ve seen a few times when the nextLine back a line of text and store a blank line to read next.

Browser other questions tagged

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