Add +1 Variable to each created object

0

I am creating a Candidate class, where I will pass a name and a candidate number. In it I created a vector, to store the candidate's number. So that you do not do not equal a candidate number. I created a variable, aux and a cont, to scan the for and thus check if that number already has. However, I type the same number and he does not accuse, I broke my head and nothing. I have printed the variables aux and cont and realized that every time I create an object, the values are always the same. I know it's to be the same, but I wanted to know a way to add +1 to each created object. So you can scan the vector and check if there is an 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);
    }
    
asked by anonymous 17.08.2017 / 00:20

1 answer

0

This can not be the case with which you are having problems. But there is a problem here:

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

The while loop is only comparing with the number in the index i , which allows the user to enter the number that has already been inserted and checked by the for loop, ie any number smaller than i within the array num .

    
17.08.2017 / 01:34