Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: How to prevent printing the same output 5 times

  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

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

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to prevent printing the same output 5 times

    Quote Originally Posted by pbrockway2 View Post
    Please use the code tags, and consider indenting with spaces which are rendered by web browsers in a much more readable way.
    ?????
    Seems pretty readable to me.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to prevent printing the same output 5 times

    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.
    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.

    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 user
    create a book array
    create 5 books and put them into the array
    do 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.
    Fubarable likes this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to prevent printing the same output 5 times

    Quote Originally Posted by gabrielpr12 View Post
    ?????
    Seems pretty readable to me.
    Yep, because pbrockway edited your post and added code tags.

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  2. System.out.println not printing, weird output
    By KonchShell in forum New To Java
    Replies: 9
    Last Post: 03-16-2011, 11:50 AM
  3. Need help with printing console output to JTextArea
    By ShinTec in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2010, 10:10 AM
  4. Having trouble looping & printing output
    By carrotcake in forum New To Java
    Replies: 1
    Last Post: 04-04-2010, 05:37 AM
  5. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 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
  •