Results 1 to 15 of 15
  1. #1
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Printing 5 Strings per line

    Hi there, I have a doubly linked list, which contains Nodes which intern contain Strings.

    I wish to print the contents of this doubly linked list 5 strings per line.

    For example: If the list contained "hello", "world", "apple", "orange", "rainbow", "red", "yellow", "cat", "fish", "house"

    the print output would be:

    hello world apple orange raindow
    red yellow cat fish house

    Any help would be great, Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Printing 5 Strings per line

    Try using a loop and a counter. Get the Strings one by one from the list, call print() 5 times and then call println() once
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    Quote Originally Posted by Norm View Post
    Try using a loop and a counter. Get the Strings one by one from the list, call print() 5 times and then call println() once
    Hey Norm, thanks for the reply.

    Is this something along the lines you were meaning?

    public void print() {
    Java Code:
    String[] wordList = getStrings();
            for(int i = 0; i < wordList.length; i++) {
                    System.out.print(wordList[i]);
                    System.out.print(wordList[i]);
                    System.out.print(wordList[i]);
                    System.out.print(wordList[i]);
                    System.out.print(wordList[i]);
                    System.out.println();
                }
        }
    Because this printed each string five times, then created a new line.

    (getStrings is a method i created which just takes all the strings from the Doubly Linked List and puts them into a string array)

    EDIT:

    Here's the received output:

    hellohellohellohellohello
    worldworldworldworldworld
    appleappleappleappleapple
    orangeorangeorangeorangeorange
    raindowraindowraindowraindowraindow
    redredredredred
    yellowyellowyellowyellowyellow
    catcatcatcatcat
    fishfishfishfishfish
    househousehousehousehouse
    Last edited by mafro; 05-03-2012 at 05:26 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Printing 5 Strings per line

    Why do you print the same String with 5 print() statements? Where is the counter to control how many Strings are printed before a new line is printed.
    Try listing the steps in pseudo code the program should take to do what you want it to do. When you get the logic worked out, then try writing the code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Printing 5 Strings per line

    You want a loop which will print a single entry, based on the index.
    So similar to your current loop, but with only the single print().
    Now, you'll need another loop around that that'll do the println() once the inner loop is done.
    Java Code:
    while my index < length of the array
       set your word count to zero
       while my count of strings < 5 and my index < length of my array
          print array[index]
          increment index and my count
       new line
    Something like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    mafro is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    Quote Originally Posted by Tolls View Post
    You want a loop which will print a single entry, based on the index.
    So similar to your current loop, but with only the single print().
    Now, you'll need another loop around that that'll do the println() once the inner loop is done.
    Java Code:
    while my index < length of the array
       set your word count to zero
       while my count of strings < 5 and my index < length of my array
          print array[index]
          increment index and my count
       new line
    Something like that.
    Thanks!! I wasn't far off with my first pseudo attempt :(

    This is the final result:

    Java Code:
    public void print() {
            String[] wordList = getStrings();
            int n = 5;
            int i = 0;
            int j = 0;
            
            for(;i < wordList.length;) {
                j = 0;
                for(; j < n && i < wordList.length;) {
                    System.out.print(wordList[i] + " ");
                    i++;
                    j++;
                }
                System.out.println();
            }
        }
    Thanks for all the help guys :)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Printing 5 Strings per line

    Both of those 'for' loops would actually look better as 'while' loops, since that's what they are now that you've stripped out the other two sections of a 'for', but that is the sort of thing I was thinking of.
    Well done for managing to translate my pseudo into something workable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Rabea is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    thanx

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Printing 5 Strings per line

    I'd use one for loop with a if testing the count of items printed on the current line
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    J-max04 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    wouldnt this work?
    Java Code:
    for(int i = 0; i < array[].length; i++)
    {
        System.out.print(array[i]);
    }
    System.out.flush();
    Last edited by J-max04; 05-04-2012 at 11:17 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Printing 5 Strings per line

    What problem are you solving? Go read what this thread was about.

    What is the last statement (#5) supposed to do?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    J-max04 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    flush prints all of the unprinted print(String)'s
    i just thought it was an easier way to do it

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Printing 5 Strings per line

    Does it write a new line character so the next printed output is on the next line?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    J-max04 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Printing 5 Strings per line

    what? if you mean are all of the strings printed on the same line, then yes.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Printing 5 Strings per line

    Quote Originally Posted by Norm View Post
    I'd use one for loop with a if testing the count of items printed on the current line
    Good point.
    Which is why I never trust my first solutions...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. New line in Strings
    By Josep_16 in forum New To Java
    Replies: 3
    Last Post: 09-07-2011, 02:33 PM
  2. How to make new-line when p rinting strings?
    By ozzyman in forum Java 2D
    Replies: 2
    Last Post: 05-04-2011, 10:24 AM
  3. Printing indexOf line
    By clrgomes in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 09:30 AM
  4. Issue with printing line
    By Azndaddy in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 08:37 PM
  5. Printing command line arguments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:27 AM

Tags for this Thread

Posting Permissions

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