-3
public Event(String name, int numberOfTries) {
    this.name = name;
    this.numberOfTries = numberOfTries;
}

public String getName() {
    return name;
}

public int getNumberOfTries() {
    return numberOfTries;
}

public String toString() {
    return name + ", " + numberOfTries;
}

public void addEvent() {
    scan = new Scanner(System.in);
    eventList.add(new Event("Jacob", 5));

    System.out.print("Event name: ");
    String name = scan.nextLine();
    boolean match = true;
    for (int i = 0; i < eventList.size(); i++) {
        if (eventList.get(i).getName().equalsIgnoreCase(name)) {
            match = false;
            if (!match) {
                System.out.println(name + " has already been added\n");
            }

            else {
                System.out.println("Event name: ");
            }
        }
    }
    System.out.print("Attempts allowed: ");
    int numberOfTries = scan.nextInt();
    scan.nextLine();

    eventList.add(new Event(name, numberOfTries));

    System.out.println(name + " added\n");

}

When I enter the pre-existing name "Jacob", it goes like this:

"Event name: Jacob Jacob has already been added

Attempts allowed:"

I want the program to ask me for a new name, since Jacob already exists. But I can't seem to go back in the loop.

I don't want to use Maps or anything like that, only ArrayLists.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Zakan
  • 51
  • 6
  • 2
    first of all: see the problem in these lines? : match = false; if (!match) { System.out.println(name + " has already been added\n"); } – Stultuske Jan 04 '18 at 12:31
  • Why not simply use `contains` to check if an element is present in a list, i.e. `eventList.contains(name)` – nbokmans Jan 04 '18 at 12:31
  • I agree with nbokmans. just add an equals that checks only on the name to your Event class. you'll have to create a new Instance of Event with a name to test though – Stultuske Jan 04 '18 at 12:33
  • So, if I understand you correctly: System.out.print("Event name: "); String name = scan.nextLine(); boolean match = true; for (int i = 0; i < eventList.size(); i++) { if (eventList.contains(name)) ?? – Zakan Jan 04 '18 at 12:35
  • Wrong construction, you are always assigning false inside match found, which probably should be true and there should be some sort of break action on match – Myszsoda Jan 04 '18 at 12:36
  • Related: [Check if a value exists in ArrayList](https://stackoverflow.com/q/4404084). See [How can I test if an array contains a certain value?](https://stackoverflow.com/q/1128723) if you want to know how to write a loop that does this (it's about arrays, not ArrayLists, but the loop looks similar). – Bernhard Barker Jan 04 '18 at 12:37
  • Got help from another student: System.out.print("Event name: "); String name = scan.nextLine(); for (int i = 0; i < eventList.size(); i++) { if (eventList.get(i).getName().equals(name)) { System.out.println("Error " + name + " has already been added\n"); return; – Zakan Jan 04 '18 at 12:50
  • 3
    Hey @Zakan, for future reference, just because we didn't fix your code dump doesn't mean we didn't help you. No need to be ungrateful. – nbokmans Jan 04 '18 at 13:12

1 Answers1

0

First of all, you can't define match=false; and then ask if(!match). It is going yo be always true.

Instead, write the if...else statement outside of the loop. And when the loop finishes It will check for the match boolean. if (!match) { System.out.println(name + " has already been added\n");} else { System.out.println("Event name: "); match = true;}

Then introduce a loop which checks for match = true so the user can try several times: while(!match){ scan = new Scanner(System.in); eventList.add(new Event("Jacob", 5)); System.out.print("Event name: "); String name = scan.nextLine(); for (int i = 0; i < eventList.size(); i++) { if (eventList.get(i).getName().equalsIgnoreCase(name)) { match = false; } } if (!match) { System.out.println(name + " has already been added\n"); eventList.removeLast();} else { System.out.println("Event name: "); match = true;} }

Remember to initiallize the match variable as false, before the while checks it.

Alex Cuadrón
  • 638
  • 12
  • 19