• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help-Java Programming

NYHoustonman

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

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

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