1

I have a file file.txt which contains data in the form

integer n
title1
authorName1 authorSurname1
...
title n
authorNamen authorSurnamen
integer m
userName1 userSurname1
...
userNamem userSurnamem

What I'm trying to do is scan the first integer, then scan the whole title line as one string, author name and surname as separate strings, then loop the process n number of times. Then I want to scan the second integer, read the username and surname as separate strings, and loop this m number of times. My current attempt (not best, can't remember how I had it before) looks like this:

public void readFile()
{
    int n=0;
    int m=0;
    while(fileReader.hasNextInt()) {
        n = fileReader.nextInt();
        for(int i=0; i<=n; i++) {
            while(fileReader.hasNextLine()) {
                 String bookTitle = fileReader.nextLine();
                 String authorFirstName = fileReader.next();
                 String authorSurname = fileReader.next();
                 ArrayList<Book>.add(new Book(bookTitle,authorFirstName,authorSurname));
                }
            }
        m = fileReader.nextInt();
        for(int i=0; i<=n; i++) {
            while(fileReader.hasNext()) {
                String userFirstName = fileReader.next();
                String userSurname = fileReader.next();
                ArrayList<User>.add(new User(userFirstName,userSurname));
            }
        }
    }
}

and I can't get it to output it as I want. I'm new to using scanner and that combined with a for loop has melted my brain a bit

pxdr0
  • 73
  • 7
  • Note that your code doesn't print anything, so you can't have any output. It also doesn't compile, so you can't even run it. You need to create a list, and then add books to that list: `List bookList = new ArrayList<>(); ... bookList.add(new Book(...));` – JB Nizet Dec 16 '16 at 08:21

0 Answers0