Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to justify text?

843804Jan 8 2005 — edited Jan 11 2005
The requirement from my teacher is, input a paragraph, do justify to the text, known as line wrap too. User will set the length of lines, known as the width of the text field.
For example, if i have a text:

Due to requests by our enterprise clients, EssentialBiz is now
offering Interactive Messaging Service (IMS) to clients who wish to
operate or run interactive SMS application using short codes. IMS is
a complete SMS gateway & interactive application solution for any
entity to offer interactive SMS services to their targeted audience.
The most popular interactive SMS services provided so far is in the
form of quiz based SMS contests (using our award winning QuizMaster
application) although we also offer customized application
development for any service that the clients want.

The output text should look like a perfect reactangle, meaning I have to make words on the right side line up perfectly. To do that, I will have to add some spaces in every line. But there are also cases where the length user set is smaller than the length of lines, then it's a more complicated case cuz i have to move the words.

Everyone understands what I'm saying here? If you find my words so confusing, feel free to ask me what I mean. Hope you will help me, many thanks first.

By the way, there's a piece of code i found online.
It could do the job, but only if the length is no smaller than the length of lines of the input text. For instance, if you change this line:
String test2 = new justifyString().formatLine(readText,68);

to be:String test2 = new justifyString().formatLine(readText,20);

It won't work anymore. But overall, i think it could be a helpful code to get some ideas.

import java.io.*;
 
public class Essential
{
public static void main (String [] args) throws IOException
{
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
FileOutputStream out = new FileOutputStream("output.txt");
PrintStream pr = new PrintStream(out);
String readText = br.readLine();
 
while (readText!=null)
{
try{
readText = br.readLine();
String test2 = new justifyString().formatLine(readText,68);
System.out.println(test2);
pr.println(test2);
}
catch(NullPointerException e)
{}
}
pr.close();
}
}
 
class justifyString
{
protected String formatLine(String line, int maxLineLength)
{
final int totalNumBlanksToAdd = maxLineLength - line.length();
int numBlanksLeftToAdd = totalNumBlanksToAdd;
int nextBlankPos = line.indexOf(" ");
// If there is no space, ths means there is only one word in the
// line. So it's OK to return right away!
if (nextBlankPos == -1) {
return line;
}
// This is a realy nice example to worry about correctness
// arguments.
 
// Start the formatted line to be the orginal line.
String formattedLine = line;
// Decomment for debugging.
//System.out.println("Initially line: \"" + formattedLine + "\"");
while (numBlanksLeftToAdd > 0) {
// Decomment for debugging.
// System.out.println("line: \"" + formattedLine + "\"");
// Insert a space after nextBlankPos.
formattedLine = formattedLine.substring(0,nextBlankPos) + " "
+ formattedLine.substring (nextBlankPos);
// One more down ...
numBlanksLeftToAdd--;
// Find the beginning of the next word; skip the rest of the
// spaces in this gap
while (formattedLine.charAt(nextBlankPos) == ' ') {
nextBlankPos++;
if (nextBlankPos > formattedLine.length()) {
nextBlankPos = 0;
}
}
// Now find the next space.
nextBlankPos = formattedLine.indexOf(" ", nextBlankPos);
// If we've reached the end, start back at the beginning.
if (nextBlankPos == -1) {
nextBlankPos = formattedLine.indexOf(" ");
}
 
}
// We're done.
// Decomment for debugging.
//System.out.println("Finally line: \"" + formattedLine + "\"");
return formattedLine;
}
 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 8 2005
Added on Jan 8 2005
6 comments
280 views