Results 1 to 15 of 15
Thread: Printing 5 Strings per line
- 05-03-2012, 04:25 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
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.
- 05-03-2012, 05:01 PM #2
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.
- 05-03-2012, 05:22 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Printing 5 Strings per line
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(); } }
(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
househousehousehousehouseLast edited by mafro; 05-03-2012 at 05:26 PM.
- 05-03-2012, 05:36 PM #4
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.
- 05-03-2012, 05:57 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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
Please do not ask for code as refusal often offends.
** This space for rent **
- 05-03-2012, 06:31 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Printing 5 Strings per line
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(); } }
- 05-03-2012, 07:02 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 05-04-2012, 12:56 AM #8
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: Printing 5 Strings per line
thanx
- 05-04-2012, 01:00 AM #9
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.
- 05-04-2012, 02:28 AM #10
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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.
- 05-04-2012, 02:38 AM #11
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.
- 05-04-2012, 02:46 AM #12
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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
- 05-04-2012, 02:58 AM #13
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.
- 05-04-2012, 10:36 AM #14
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
Re: Printing 5 Strings per line
what? if you mean are all of the strings printed on the same line, then yes.
- 05-04-2012, 11:03 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Similar Threads
-
New line in Strings
By Josep_16 in forum New To JavaReplies: 3Last Post: 09-07-2011, 02:33 PM -
How to make new-line when p rinting strings?
By ozzyman in forum Java 2DReplies: 2Last Post: 05-04-2011, 10:24 AM -
Printing indexOf line
By clrgomes in forum New To JavaReplies: 2Last Post: 03-06-2011, 09:30 AM -
Issue with printing line
By Azndaddy in forum Advanced JavaReplies: 1Last Post: 04-04-2008, 08:37 PM -
Printing command line arguments
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 10:27 AM
Bookmarks