0

My While loop is only executing once and it appears it moves back to the top to run through the loop again but only completes the System.out.println("Title"); and then moves on to execute the switch case. It appear that if I change the int pages = scanner.nextInt(); and the int pub = scanner.nextInt(); to int pages = Integer.valueOf(scanner.nextLine()); and int pub = Integer.valueOf(scanner.nextLine()); the loop works as expected. I was wondering if anyone could explain why that is?

Code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        ArrayList<Book> bookList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        boolean flag = true;

        while (flag) {

            System.out.println("Title:");
            String title = scanner.nextLine();

            if (!title.isEmpty()) {
                System.out.println("Pages:");
                int pages = scanner.nextInt();
                System.out.println("Publication year:");
                int pub = scanner.nextInt();
                Book info = new Book(title, pages, pub);
                bookList.add(info);

            } else if (title.isEmpty()) {
                flag = false;

            }

        }
        System.out.println("What informaiton will be printed?");
        String choice = scanner.nextLine();
        switch (choice) {

            case "everything":
                if (choice.equals("everything")) {
                    for (Book info : bookList) {
                        System.out.println(info.getTitle() + ", " + info.getPages() + " pages, " + info.getYear());
                    }
                }

            case "name": {
                if (choice.equals("name")) {
                    for (Book infor : bookList) {
                        System.out.println(infor.getTitle());
                    }

                }
            }

        }

    }
Jens
  • 67,715
  • 15
  • 98
  • 113
doug22
  • 77
  • 1
  • 8

2 Answers2

0

I believe what you are looking for is an explanation of the Scanner and its methods in Java. A bit of the search around stackoverflow helped me found this which explains it.

Further I have found that new Developers at time ignore the most obvious area of information that is the official docs which are always helpful to the beginners because once you can read them they make your life much more easier.

To add onto the answer I have borrowed from the mentioned link

nextLine() reads the remainder of the current line even if it is empty.

nextInt() reads an integer but does not read the escape sequence "\n".

next() reads the current line but does not read the "\n".

And you can find this on the documentation as well.

DevX
  • 308
  • 5
  • 16
0
while (true) {

            System.out.println("Title:");
            scanner = new Scanner(System.in);    // new loop - new scanner 
            
            String title = scanner.nextLine();
            if (title.isEmpty()) break;   // exit from loop if entered title is empty

            System.out.println("Pages:");
            int pages = scanner.nextInt();
            System.out.println("Publication year:");
            int pub = scanner.nextInt();
            Book info = new Book(title, pages, pub);
            bookList.add(info);
        }
Алексей Р
  • 7,507
  • 2
  • 7
  • 18