You have an extraneous semicolon after your if, which effectively makes it

if (x.equals("0")) { }
break;
Answer from Jon Newmuis on Stack Overflow
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ how-to-loop-arraylist-in-java
How to loop ArrayList in Java
In this post we are sharing How to loop ArrayList in Java.There are four ways to loop ArrayList: For Loop, Advanced for loop, While Loop and Iterator..
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csawesome โ€บ Unit7-ArrayList โ€บ topic-7-3-arraylist-loops.html
7.3. Traversing ArrayLists with Loops โ€” CSAwesome v1
ArrayLists can be traversed with while loops and both regular and enhanced for loops much the same way we use those constructs to loop over an array.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 03 โ€บ how-to-loop-arraylist-in-java-code.html
5 Ways to Loop or Iterate over ArrayList in Java?
You can not just print but do whatever ... Another cool approach of looping ArrayList is using Iterator in combination of while loop and traverse until you get to the end of ArrayList....
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ different ways to iterate an arraylist
Different Ways to Iterate an ArrayList
January 12, 2023 - Java program to iterate through an ArrayList of objects using a while loop. ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") ); int index = 0; while (namesList.size() > index) { System.out.println(namesList.get(index++)); } Java program to iterate through an ArrayList of objects with Java 8 stream API.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_howto_loop_through_arraylist.asp
Java How To Loop Through an ArrayList
How Tos Add Two Numbers Swap Two ... Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String ...
๐ŸŒ
Java Tutorial HQ
javatutorialhq.com โ€บ java tutorial โ€บ example source code โ€บ java array examples โ€บ loop through an arraylist
Java Loop Arraylist Example
October 6, 2019 - On this example, we basically just iterate through a List of Integer using while loop and then print each individual elements. package com.javatutorialhq.java.examples; import java.util.ArrayList; /* * This example source code on how to deal * with ArrayList using while loop */ public class WhileLoopArrayList { public static void main(String[] args) { // initialize our ArrayList ArrayList<Integer> listNumbers = new ArrayList<Integer>(); // put contents on our list listNumbers.add(7145); listNumbers.add(487); listNumbers.add(-1032); listNumbers.add(454); // iterate through our list int index = 0; int maxIndex = listNumbers.size(); while (index < maxIndex) { // print member of our arraylist one by one System.out.println(listNumbers.get(index)); index++; } } }
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterating-arraylists-java
Iterating over ArrayLists in Java - GeeksforGeeks
import java.util.ArrayList ; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating and initializing the ArrayList // Declaring object of integer type ArrayList<Integer> al = new ArrayList<Integer>(); // Adding elements to ArrayList // using add() method al.add(3); al.add(1); al.add(7); al.add(20); al.add(5); // Step 1: Setting and initializing a variable // as per syntax of while loop // Initially declaring and setting int val = 0; // Step 2: Condition // Till our counter variable is lesser than size of // ArrayList while (al.size() > val) { // Printing the element which holds above // condition true System.out.println(al.get(val)); // Step 3: Terminating condition by incrementing // our counter in each iteration val++ ; } } } Output ยท
Published ย  January 19, 2026
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to loop arraylist in java
How to loop ArrayList in Java - Mkyong.com
August 29, 2012 - package com.mkyong.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListLoopingExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Text 1"); list.add("Text 2"); list.add("Text 3"); System.out.println("#1 normal for loop"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("#2 advance for loop"); for (String temp : list) { System.out.println(temp); } System.out.println("#3 while loop"); int j = 0; while (list.size() > j) { System.out.println(list.get(j)); j++; } System.out.println("#4 iterator"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }
๐ŸŒ
Javainsimpleway
javainsimpleway.com โ€บ arraylist-with-looping
Arraylist with Looping | Javainsimpleway
In the above example, we have used while loop to iterate arraylist ... import java.util.*; public class ArrayListIterator { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("While with iterator"); Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } } Output While with iterator 14 7 21 28
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ how to iterate arraylist in java
How to Iterate ArrayList in Java - Scientech Easy
May 12, 2025 - Output: [20, 25, null, 30, 25] Iteration using while loop 20 25 null 30 25 ยท The iterator() method is used to iterate over elements of ArrayList in Java. It is useful when we want to remove the last element during iteration.
๐ŸŒ
Crunchify
crunchify.com โ€บ java j2ee tutorials โ€บ how to iterate through java list? seven (7) ways to iterate through loop in java
How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java โ€ข Crunchify
December 28, 2025 - For Loop: 0.014s For Loop Object: 0.218s While Iterator: 0.021s While Loop: 0.01s Stream ForEach: 0.361s ... Instant start = Instant.now(); Instant end = Instant.now(); // create list List crunchifyList = new ArrayList(); for(Long i=0L; i For Loop Example.โ€); start = Instant.now(); for(int i=0;i Advance For Loop Example..โ€); start = Instant.now(); for(String temp:crunchifyList){ //System.out.println(temp); } end = Instant.now(); long t2 = Duration.between(start, end).getNano();
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ iterate-through-arraylist-in-java
Iterate through ArrayList in Java
July 28, 2025 - Iterating through ArrayList using while loop: 10 20 30 40 ยท The forEach loop is similar to the for loop, but it is easier to read. It allows us to iterate through each element of the ArrayList without an index variable.
Top answer
1 of 2
5

Separate the logic of asking the question for more information from that of updating the list

String decision = "No";
do {
    System.out.println("Would you like to add another book?");
    decision = keybd.nextLine();
    if (decision.equals("Yes")) {
        // Get information about book
        String anyTitle = keybd.nextLine();
        String anyAuthor = keybd.nextLine();
        addBooks(anyTitle,anyAuthor);
    }
} while (decision.equals("Yes"));

You could go a step further, making a addBook method which prompts the user for the informaiton...

public void addBook() {
    String anyTitle = keybd.nextLine();
    String anyAuthor = keybd.nextLine();
    addBooks(anyTitle,anyAuthor);
}

Then you could just call addBook from the while-loop

do {
    System.out.println("Would you like to add another book?");
    String decision = keybd.nextLine();
    if (decision.equals("Yes")) {
        addBook();
    }
} while (decision.equals("Yes"));

Further decoupling your code, allowing you to call addBook whenever you want to prompt the user for information about the book and adding it to the list

2 of 2
2

If I understand your various snippets of code, I would strongly recommend you consider something that calls your method setOneBook... but I suggest you change the method visibility (and name) -

private void addOneBook(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
}

This way you have used encapsulation to hide the method that adds a book. Next, change the public method. You are adding multiple books, so don't pass in a title or an author -

public void addBooks() {
    do {
        String anyTitle = keybd.nextLine();
        String anyAuthor = keybd.nextLine();
        addOneBook(anyTitle, anyAuthor);
        System.out.println("Would you like to add another book?");
    } while (keybd.next().equalsIgnoreCase("yes"));
}
๐ŸŒ
Quora
quora.com โ€บ How-do-I-use-loop-in-Arraylist-to-give-input-in-java
How to use loop in Arraylist to give input in java - Quora
Answer (1 of 2): To traverse array list follow these: import java.util.List; import java.util.ArrayList; import java.io.*; public class test { public static void main(String args()) { int arr[] = new int[10]; arr={1,2,3,4,5,6,7,8,9,10}; List list = new ArrayList (); fo...