Results 1 to 2 of 2
  1. #1
    justinm231 is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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++;
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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:

    Java 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.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2011, 04:46 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  4. Simple Inheritance issue...
    By AWE in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 09:56 PM
  5. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •