Add +1 Variable to each created object

Asked

Viewed 713 times

1

I’m creating a candidate class, where I’ll pass on a candidate’s name and number. In it I created a vector, to store the candidate’s number. So that there is no equal candidate number. I created a variable, aux and a cont, to scan the is and so check if that number already has. However, I type equal number and he does not accuse, I broke my head and nothing. I had the variables aux and cont printed out and I realized that every time I create an object, the values are always the same. I know it’s meant to be equal, but I wanted to know a way to add +1 to each created object. To scan the vector and check if there is equal candidate number.

import java.util.Scanner;

public class Candidato {

    Scanner s = new Scanner(System.in);

    private String nome;
    private int numeroCand;
    private int votos =0;
    private int num[] = new int[100];
    private static int cont =1;
    private static int aux=0;

    Candidato(){
        System.out.println("Digite o nome do candidato");
        this.setNome(s.nextLine());
        System.out.println("Digite o número do candidato");
        this.setNumeroCand(s.nextInt());
        verificarNum(this.getNumeroCand());

    }

    public void verificarNum(int n){

        if(cont == 1){
            num[cont-1] =numeroCand;
            aux++;
            cont++;

            System.out.println("Candidato cadastrado com sucesso!");
            System.out.println("_________________________________________________________________");

        }else{

            for(int i=0;i<=aux;i++){
                if(this.getNumeroCand() == num[i]){
                    System.out.println("Número válido, pois esse já é de outro candidato.");
                    while(this.getNumeroCand() == num[i]){
                        System.out.println("Digite o número do candidato");
                        this.setNumeroCand(s.nextInt());
                    }
                }
            }
        }




        /*for(int i=0;i<cont;i++){
                if(this.getNumeroCand() == num[i]){
                System.out.println("Digite um número válido, pois esse já é de outro candidato.");

                    }
                }else{
                    System.out.println("Candidato cadastrado com sucesso!");
                    System.out.println("_________________________________________________________________");
                    num[cont-1] =numeroCand;
                    }
            num[cont-1] =numeroCand;

        }
        cont++;*/
        System.out.println("Cont: " + cont + "aux: " + aux);
    }
  • If it is one candidate because there is a vector with numbers in it? It seems to me that the class is conceptually wrong, then everything you do will be potentially wrong. You can even find a solution for the question, but it will be to put the arrow of a car that doesn’t even walk right.

1 answer

0

It may not be the case that you are having problems with. But there is a problem here:

 while(this.getNumeroCand() == num[i]){
                    System.out.println("Digite o número do candidato");
                    this.setNumeroCand(s.nextInt());
 }

While loop is only comparing with number in index i, which enables the user to enter the number that has already been entered and checked by the loop for, or any number less than i inside the array num.

Browser other questions tagged

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