How can I make sure that when I enter an item in a java fix, first check if it is found?

0

// Here I have the entry but how could I verify it;

Clase []c=new Clase[10];
int i=0;
do{
   System.out.println("Ingrese numero de cuenta");
   c[i]=entrada.readLine();
}while(i<c.length);

i +;

    
asked by Enrique 10.08.2018 в 15:39
source

2 answers

1

There is an easy way with java utils.

Clase []c=new Clase[10];
int i=0;
do{
   System.out.println("Ingrese numero de cuenta");
   String line = entrada.readLine(); 
   if(java.util.Arrays.asList(c).indexOf(line) == -1) {  
     System.out.println("El número de cuenta ya existe");
   } else {
     c[i] = line;
   }
}while(i<c.length);

asList is a static method that temporarily converts an array to be able to take advantage of all the advantages offered by Java lists.

IndexOf obtains the index where that element is found, if it does not exist, it returns -1

More information link

    
answered by 10.08.2018 в 16:23
0

I could implement a for for search cycle after the input of the data and before entering it into the array.

Clase []c=new Clase[10];
int i=0;
do{
  System.out.println("Ingrese numero de cuenta");
  int value = entrada.readLine();

  for(int ii=0; ii < c.length(); ii++)
  { 
    if(value != c[ii])
    {
     c[i]= value;
    }
  }
i++:
}while(i<c.length);

I hope it helps you. Greetings.

    
answered by 10.08.2018 в 16:05