Results 1 to 5 of 5
- 01-13-2012, 09:03 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
How to prevent printing the same output 5 times
I am trying to write a code that asks the user for the title of a book, then the author and the number of pages, 5 times. Then I want to display all 5. The problem I am having is that when I ask for the first set of information, I get a print out of 5 of the same. I know the problem has to do with the for loop I wrote when asking for the user input. How do I do to only display one set a time. Hope you can understand what I am trying to say. Here is my code:
Java Code:import java.util.*; import javax.swing.*; import java.util.Collections; public class LibraryBookSort2 { public static void main(String[] args) { ArrayList bookTitle = new ArrayList(); ArrayList bookAuthor = new ArrayList(); ArrayList bookPages = new ArrayList(); String title; String author; String pageNumber; Integer numberOfPages; final int LIMIT = 5; for(int x = 0; x < LIMIT; ++x) { title = JOptionPane.showInputDialog(null, "Enter a book title"); bookTitle.add(title); author = JOptionPane.showInputDialog(null, "Enter the book's author"); bookAuthor.add(author); pageNumber = JOptionPane.showInputDialog(null, "Enter number of pages"); bookPages.add(pageNumber); LibraryBook[] book = new LibraryBook[5]; numberOfPages = Integer.valueOf(pageNumber); book[0] = new LibraryBook(title, author, numberOfPages);//I am trying to only get the first input book[1] = new LibraryBook(title, author, numberOfPages);//...second input book[2] = new LibraryBook(title, author, numberOfPages);//and so on book[3] = new LibraryBook(title, author, numberOfPages); book[4] = new LibraryBook(title, author, numberOfPages); for (int i = 0; i < book.length; ++i) System.out.println(book[x].getBookTitle() + "; " + book[x].getBookAuthor() + "; " + book[x].getBookPageCount()); } } }Last edited by pbrockway2; 01-13-2012 at 09:20 PM. Reason: code tags added
- 01-13-2012, 09:22 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How to prevent printing the same output 5 times
Please use the code tags, and consider indenting with spaces which are rendered by web browsers in a much more readable way.
- 01-13-2012, 09:28 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
- 01-13-2012, 09:43 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How to prevent printing the same output 5 times
You're right - you need to think more about that loop. In fact it's not so much how many books you are printing at a time, but how many books and arrays you are creating.I know the problem has to do with the for loop I wrote when asking for the user input. How do I do to only display one set a time.
Schematically what you are doing looks like this:
create title, author and pages lists
do the following LIMIT times:
get title, author and page from the usercreate a book arraycreate 5 books and put them into the arraydo the following for each book in the array:print the book information
It seems to me that far too many things are being created. For instance you create 5*LIMIT books altogether.
Do you really need both the lists and the array? If so, why - for what purpose? (do nothing without a purpose). If not, then my advice would be to use the array and remove the lists.
Rethink how you are going about the two separate tasks of (1) getting information from the user and (2) printing the book information. Specifically ask your self: "How many books should be created each time around the x= loop?" There should be just that many "new" statements. And "When should the book array be created?" If it makes sense to have the array before you start getting information from the user, then create it before the loop starts.
Have a plan. It really doesn't matter if it is a good plan, or even whether it is an effective plan (it can always be corrected). But have a plan, and be able to explain it in real life language before you start writing code.
-----
I realise that I have (deliberately) just given you things to think about. Do that, but post back with progress if you need to.
-
Re: How to prevent printing the same output 5 times
Similar Threads
-
Implement Custom Printing Logic & Enhanced Word Documents Printing
By sherazam in forum Java SoftwareReplies: 0Last Post: 12-26-2011, 03:52 PM -
System.out.println not printing, weird output
By KonchShell in forum New To JavaReplies: 9Last Post: 03-16-2011, 11:50 AM -
Need help with printing console output to JTextArea
By ShinTec in forum AWT / SwingReplies: 4Last Post: 06-04-2010, 10:10 AM -
Having trouble looping & printing output
By carrotcake in forum New To JavaReplies: 1Last Post: 04-04-2010, 05:37 AM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks