Conceptual Issue? Please Help, should be simple.
I have the following code in which I am trying to read from a text file of the format below:
42
John Smith
Java Failure
1993
...
(and so on... but first 4 are all I care about right now)
Book is a class that takes parameters (String, String, int). In order to read the first four lines into ArrayList kindle when I loop, I scan the first line and assign it as the ID... Here is where the rub comes in-- If I am trying to initiate Book, when I have the line:
Book b = new Book(fileScanner.nextLine(), fileScanner.nextLine(), fileScanner.nextInt());
...Will the two fileScanners, read lines 2 and 3 as I desire or just the same line twice? I kind of assume, it reads from left to right, as is usual, providing me with the values I desire.
CODE:
try
{
Scanner fileScanner = new Scanner(new FileInputStream("MyTextFile.txt"));
String id = fileScanner.nextLine();
int count = 0;
while( fileScanner.hasNextLine() && count< 3 )
{
Book b = new Book(fileScanner.nextLine(), fileScanner.nextLine(), fileScanner.nextInt());
CatalogItem<String, Book> c = new CatalogItem<String, Book>(id, b);
kindle.add(c);
count++;
}
Re: Conceptual Issue? Please Help, should be simple.
If I understand your question, then yes, you understand it correctly. If you have a line of code like this:
Code:
Book b = new Book(fileScanner.nextLine(), fileScanner.nextLine(), fileScanner.nextInt());
...it will make three calls to methods in your fileScanner object, and then construct a Book with the return values.