Results 1 to 3 of 3
Thread: Need Help!!!
- 12-10-2008, 04:30 AM #1Member
- Join Date
- Dec 2008
- Rep Power
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 Book2).
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:
The Ground Beneath Her Feet
Song of Solomon
The Illustrated Man
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!");
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???
- I dont even know where to start
how do you read text from a file and store it in an array list?
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.
- 12-10-2008, 04:44 AM #3
- Join Date
- Jul 2007
- Colombo, Sri Lanka
- Blog Entries
- Rep Power