0

If the user key in 4 I wish to pop up a message to inform the user that is no data found for user id "4" in the array list.

But when the user key in 4 there is 3 no data found appear.

    User usr1 = new User(1,"Ken", 55.5, 26, Arrays.asList("0140392812", "0123456789"));
    User usr2 = new User(2, "Mark", 54.7, 33, Arrays.asList("0129876543"));
    User usr3 = new User(3, "Ong", 62.3, 34, Arrays.asList("06123456", "0987654322", "01798654321"));

    ArrayList<User> ulist = new ArrayList<User>();

    ulist.add(usr1);
    ulist.add(usr2);
    ulist.add(usr3);



String answer ="";
    do{
        Scanner scan = new Scanner(System.in);
        System.out.println("Please Enter user ID");
        int userid = scan.nextInt();
        for(User uid: ulist){               

            if(userid == uid.getUID()){
                System.out.println(uid.getUID() +", " + uid.getName() +", " + uid.getAge() +" years old, " + uid.getWeight() +"kg");

            }else{
                System.out.println("no data found");
            }

        }
        System.out.println("Continue(Y/N)");
        answer = scan.next();
    }while(answer.equalsIgnoreCase("y"));

current result:

no data found

no data found

no data found

Result that i wish:

no data found

Baka Yaro
  • 3
  • 2
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – domsson Mar 18 '17 at 17:59
  • Not the cause of your issue, but I advice you take the first line out of your loop - there is no reason to create a new scanner with every iteration. – domsson Mar 18 '17 at 18:00

3 Answers3

1

Remove else { System.out.println("no data found"); } from the loop and put it outside. The for loop intent is to find, to lookup the correct user.

Once found you may use it: you have to declare a variable of type User before loop, initialized to null and if it's null after lookup, you have to print the message 'not found'.

Aubin
  • 13,790
  • 8
  • 55
  • 78
  • It's your job, not mine. Do a try, post it, and if it's wrong this community may help you. – Aubin Mar 19 '17 at 09:28
0

You get no data found 3 times because you print it in the cycle: for every user in the list, if his id doesn't match specified id, System.out.println("no data found"); You can avoid this modifying your cycle or writing a method that fidns user by id.

User found;
for (User uid : ulist) {               
  if (userid == uid.getUID()) {
    found = uid;
    break; // assuming that ids are unique
  }
}
if (found != null) {
  System.out.println(found.getUID() +", " + found.getName() +", " + found.getAge() +" years old, " + found.getWeight() +"kg");
} else {
 System.out.println("no data found");
}
Viacheslav Z
  • 650
  • 4
  • 10
-1

You have problem in your logic. in your for loop you should have a break statement, in case there is no data found.

else { System.out.println("no data found"); break; }

Jason1
  • 71
  • 4