-3

Figured it out... I can't believer it took me that long just to get this:

        int id = inputFile.nextInt();
        String title = inputFile.next();
        String isbn = inputFile.next();
        String author = inputFile.next();
        String category = inputFile.next();
        char catagories = category.charAt(0);
trama
  • 1,271
  • 3
  • 14
  • 24
  • Stackoverflow is not for getting your homework done... :-/ – fortran Mar 02 '13 at 18:29
  • I understand that. I was hoping to be push in the right direction. And @zvzdhk, I am not sure if I am doing the Book readInBooks = new Book.... correctly. – trama Mar 02 '13 at 18:32
  • What happens when you run your program? Does it work? If not, what are you expecting it to do, and what does it do instead? – JB Nizet Mar 02 '13 at 18:32
  • Consider reading each line in one at a time with `inputFile.nextLine()`, then consider splitting the String returned with String's `split(" ")` method and using the array tokens returned in your constructor parameters. – Hovercraft Full Of Eels Mar 02 '13 at 18:33
  • why do people keep asking the same questions - here's a read line by line question for you - http://stackoverflow.com/questions/5800361/quickest-way-to-read-text-file-line-by-line-in-java – radai Mar 02 '13 at 18:34
  • @JB Nizet it does not work. I am expecting it to do what I put in the description at the very, very end. And it crashes. "Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source)" – trama Mar 02 '13 at 18:35
  • @radai: I don't think that your link will help the original poster out much. Yes the titles are similar, but the problems are different. His main problem is translating the data held by each row of Strings in the file into an object. – Hovercraft Full Of Eels Mar 02 '13 at 18:36
  • 1
    @trama: think of what happens when you call `nextLine()` when all you want is a single token on the line and when each row of data holds multiple tokens, each a component of the Book's information. You should logically be able to figure out what you're doing wrong here. This is more a matter of simply thinking through the problem, which you're not doing. – Hovercraft Full Of Eels Mar 02 '13 at 18:37

1 Answers1

1

The input is

96385 This_Book 0998706482 Smith Non-Fiction

But you're reading it using

 new Book(inputFile.nextInt(), inputFile.nextLine(), inputFile.nextLine(), inputFile.nextLine(), inputFile.nextLine())

So you're reading an int, then a whole line, then a whole line, then a whole line, then a whole line.

Use next() instead of nextLine(), since you want the next token, and not the next line.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I tried doing that, but it asks me to change the the Book constructor to (int, String, String, String, String). I need the last one to be a char. – trama Mar 02 '13 at 18:43
  • 1
    @trama: You can easily extract a char from a String using one of String's methods, `charAt(...)`. 1+ to the correct answer. – Hovercraft Full Of Eels Mar 02 '13 at 18:59
  • 1
    It doesn't make sense. The last argument is *already* a String, since you're passing `input.nextLine()` which is a String. If you want to pass a char, then transform the last token into a char. – JB Nizet Mar 02 '13 at 18:59