0

does anyone know why "Wrong!!!" is printing out 6 times? Is this something to do with my array list as it contains 6 person ticket details in it. Thank-you in advance...

public class Method1 {

public static void main(String[] arg) {
        Method1 sc = new Method1();
        sc.run();
    }

    private void run() {
        PersonData p = new PersonData();
        List<PersonType> personDetailsList = (List<PersonType>) p.getList();
        int input;
        try {
            do {
                Scanner in = new Scanner(System.in);
                System.out.println("Enter person ticket number");
                input = in.nextInt();
                for (PersonType q : personDetailsList) {
                    if (q.getPersonNumber() == input) {
                        System.out.println("Person Ticket Number: " + q.getPersonNumber() + "\n" 
                                + "Person Ticket Name: " + q.getPersonName() + "\n");
                        break;
                    }

                    else if (q.getPersonNumber() != input) {
                    System.out.println("Wrong!!!");     

                    }
                }
            } while (input != -1);
            System.out.println("Bye");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
  • 1
    Provide the code of PersonData, please. – Florian Schaetz Mar 14 '16 at 17:12
  • 1
    You're printing it for each person in the list where the `getPersonNumber` doesn't return the same number as entered. – Tom Mar 14 '16 at 17:12
  • 1
    Presumably none of the person-numbers in your list are equal to the number you input. Note that if there are 6 people and your input does match the person-number of one of them, you will still output "Wrong!!!!" five times (unless they all have the same person-number). – FredK Mar 14 '16 at 17:15
  • Why isn't the return type of `PersonData.getList()` already `List`? Why the cast? – Andy Turner Mar 14 '16 at 17:16
  • This question reminds http://stackoverflow.com/q/35992464/1393766. If both are yours why bother creating and posting from few accounts? – Pshemo Mar 14 '16 at 17:16
  • Why `else if (q.getPersonNumber() != input)` when you have already checked whether they equal? – FredK Mar 14 '16 at 17:16

1 Answers1

0

try this

private void run() {
        PersonData p = new PersonData();
        List<PersonType> personDetailsList = (List<PersonType>) p.getList();
        int input;
        boolean flag = false;
        try {
            do {
                Scanner in = new Scanner(System.in);
                System.out.println("Enter person ticket number");
                input = in.nextInt();
                for (PersonType q : personDetailsList) {
                    if (q.getPersonNumber() == input) {
                        System.out.println("Person Ticket Number: " + q.getPersonNumber() + "\n" 
                                + "Person Ticket Name: " + q.getPersonName() + "\n");
                        flag=true;                           
                        break;
                    }
                }
                if(!flag){
                    System.out.println("Wrong!!!");     
                }
            } while (input != -1);
            System.out.println("Bye");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
Marcin D
  • 918
  • 7
  • 14