Results 1 to 3 of 3

Thread: Need Help!!!

  1. #1
    idontknow13 is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Need Help!!!

    Write a class which deals with Books: every Book has a title and an author. (You'll need a constructor and two getters, a toString method, but no setter methods.)

    First, your program should read the title and author of books from a file1, storing the result in a List<Book> (or, in a Book[]2).

    To create the data file, you can save this link to disk, or create the file yourself using any text editor (BlueJ or any other -- if using something like Microsoft Word be sure to File > Save as…, using the format “Text Only”! Be sure not to include any blank lines, or spaces at the start/end of each line). Here is a sample:
    Catch-22
    Joseph Heller
    Midnight's Children
    Salman Rushdie
    Fahrenheit 451
    Ray Bradbury
    Beloved
    Toni Morrison
    Satanic Verses
    Salman Rushdie
    The Ground Beneath Her Feet
    Salman Rushdie
    Song of Solomon
    Toni Morrison
    The Illustrated Man
    Ray Bradbury


    Then, prompt the user to type in an author's (full) name;
    Your program should create a list (or array3) containing all the Books whose author has the given name, and finally display that list on the screen.
    (For style points, print something like “no books by author”, if we don't have any books by that author.)

    GUI I/O and Stand-alone applications
    We can convert this program to one that we can double-click. However, first we'll need to make several changes. Create a second project, separate from the previous problem, and copy everything over.

    Unfortuately, when starting a java program by double-clicking it, the program doesn't have a console window, so we can't use System.out nor System.in! Instead, we'll use GUI dialog boxes. We'll need to replace every System.out.println and every reading-from-the-keyboard with the following two methods instead: /* Show a message. (Always use null for the first argument.) */
    void javax.swing.JOptionPane.showMessageDialog( null, String msg )

    /* Read a line of input, after displaying a prompt: */
    String javax.swing.JOptionPane.showInputDialog( String prompt )

    Go ahead and try calling these from the Code Pad, first: javax.swing.JOptionPane.showMessageDialog( null, "Hello there everybody!");

    String answer;
    answer = javax.swing.JOptionPane.showInputDialog( "What is your quest?");

    answer // look at the result, in Code Pad.



    Be sure that main starts your whole program running — that's what will be called when your program is double-clicked.

    From BlueJ (with the main Project window active), choose Project > Save Jar file…. You'll be prompted for which class's main to call upon double-clicking. BlueJ will then create a file (whose name ends in “.jar”); when you double-click that file, your program will run, ta-dum!



    I dont even know where to start...................... how do you read text from a file and store it in an array list???

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I dont even know where to start
    By breaking the problem down into small steps and start doing each small step, one at a time.

    how do you read text from a file and store it in an array list?
    Step 1: open the text file and create an ArrayList (OK, two steps)
    Step 2: while there are still lines available, read a line from the file.
    Step 3: use this data to create a Book object
    Step 4: Place this new Book into the ArrayList
    Step 5: repeat steps 2, 3, and 4 while there's still lines left
    Step 6: close file

    As for reading in text from a file, check out these tutorials:
    Sun General I/O Tutorial
    Sun Character Stream (text) I/O Tutorial
    Sun Scanner Tutorial

    Have a look, give it a try, and come back with your code if you have further questions. Good luck.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by idontknow13 View Post
    I dont even know where to start...................... how do you read text from a file and store it in an array list???
    Do you know anything about Java? If not I think it's better to workout on Suns' tutorial first of all. Then have a try with this project.

Posting Permissions

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