简体   繁体   中英

Foreach loop only working in order for elements stored inside ArrayList

i've an ArrayList of products "Snacks" that are being read from a file and stored inside this ArrayList, displaying their CODES,NAMES,PRICES. using this code, named selectProduct() method:

public void selectProduct() {
 loadProducts();
    System.out.println("Insert product code");
    Scanner keyboard = new Scanner(System.in);
    for(Snack s : arraysnack){
        if(keyboard.next().equals(s.code))
            System.out.println("Product found");
        else 
            System.out.println("Product NOT found");
               break;
    }

They ArrayList is correctly populated from the method "loadProducts" , but when i try to give a code input , if it follows the order of elements stored it prints correctly, else it will skip and the foreach loop breaks automatically when i reach the last product. for Example the array is populated like this(Code,Name, Price)

PATA1   san carlo   0.4
PATA2   chipsters   0.35
KIND1    kinder 0.75
KIND2    kinder 0.5
KIND3    hippo  0.25
MARS1    mars   0.8

If i Type the code in order it gives a correct output(but always ending after 6 iterations) , but if i skip the first, the Output gives "product find" only if i type the second... I want a "Product find" output if i print one of the 6 codes stored in the arraylist, also if the order is not respected, and i want to iterate more than 6 times ( because there are only 6 products).

您可以简单地使用list.contains()而不是按顺序遍历list.contains()

for(Snack s : arraysnack){
        if(keyboard.next().equals(s.code))

In each iteration, you request a new input from keyboard. This is wrong! You should get the input once before the loop.

boolean isFound = false;
String userCode = keypord.next();
for(Snack s : arraysnack){
    if(userCode.equals(s.code)){ // <- Especially for beginners, I recommend ALWAYS use brackets!
        System.out.println("Product found");
        isFound = true;
        break; // Break here! It has been found.
    }
    //else 
        // Here you only know that it isn't the current product!
        // System.out.println("Product NOT found");
        //    break;  <- this break would have been outside if/else !!
}
if( !isFound ) System.out.println("Product NOT found");

Sidenote: brackets - use them!

This :

if(keyboard.next().equals(s.code))
    System.out.println("Product found");
else 
    System.out.println("Product NOT found");
    break;

is equivalent to this:

if(keyboard.next().equals(s.code)){
    System.out.println("Product found");
}else{
    System.out.println("Product NOT found");
}
break;

So the break will happen always - whatever the outcome of the condition was.

Use .nextLine() instead of just next() to be sure the scanner chunks the input data by the newline delimiter. Also, you didn't use brackets on you if statement and then the else was followed by two lines of code. If you don't use the brackets, only the line following the "else" will be part of the else-block. Everything following this line will be run independently from the if-statement - that's why it just breaks. Additionally, you probably only want one input per iteration.

Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
for(Snack s : arraysnack){
    if(input.equals(s.code)){
        System.out.println("Product found");
    } else { 
        System.out.println("Product NOT found");
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM