Need help-Java Programming

NYHoustonman

Platinum Member
Dec 8, 2002
2,642
0
0
Aight, I have to make a simple text formatter in java. It must read one line of test and print it in one or more lines of a user specified width, and center it (keeping whole words together, but not factoring in words that would require indentation). I know that removing all of the white space was recommended... I don't want this program written for me, I'm just looking for recommendations as to how to go about doing this... Cause I'm lost 0.o.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Write some code that writes HTML based on want the user wants (i.e., Use center tags and nonbreaking spaces,
Use a JEditorPane to render it.
Sit back and admire the ease in which this was done.

 

NYHoustonman

Platinum Member
Dec 8, 2002
2,642
0
0
I probably also should have mentioned that I'm supposed to format it in a way that I define my own methods, so I don't know how far from the norm I could go... and how far that would take me (cause I have no idea wtf that is :p).
 

znaps

Senior member
Jan 15, 2004
414
0
0
Create methods to count the characters (Character.isLetter() method) , white space, numbe of words, and use that to create your formatting. I suspect this is just command line stuff, so no GUI required. Check out the static methods of the Character class.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: NYHoustonman
I probably also should have mentioned that I'm supposed to format it in a way that I define my own methods, so I don't know how far from the norm I could go... and how far that would take me (cause I have no idea wtf that is :p).

JEditorPane

What you do is you have a String that holds all the HTML you've built up so far.

Let's call is String s.

Say you have some text you would like centered....

You simply concatenate it onto 's' surrounded by center tags
like....
s += "<center>" + text + "</center>";

Now say you wanted to add a page break, just concatentate a sole "</br>" to s.


Now when you're done, constuct a JEditorPane.

JEditorPane jep = new JEditorPane("text/html", s) <-- you're passing s as the text you would like to be displayed as HTML

Now you can simply add it to yourn Container or whatever.
 

NYHoustonman

Platinum Member
Dec 8, 2002
2,642
0
0
Aight, I'm lost again lol... I've gotten a method to count the number of characters, spaces, and words, but I'm suffeing from something akin to writer's block. I know it's going to be some kind of loop or something... I think... And I can't break a line in the middle of a word...