?
Solved

how to justify text

Posted on 2004-04-20
12
Medium Priority
?
1,453 Views
Last Modified: 2012-08-13
hello...

I have a problem... how to justify text using java. Actually i want to format
the text into left and right justify. For example (text below) :
 +++++++++++++++++++++++++++++++++++++++
    The Java 2-D API counts among the most significant recent additions to the Java platform. The API makes it relatively easy for the programmer to treat hardware devices (including video displays and printers) as abstractions, and simplifies the process of conceiving, manipulating, and displaying geometric figures.
++++++++++++++++++++++++++++++++++++++++

Now, I want the text to left and right justify format...then want to save this
text in file .txt. But, I don't know how to do it..I hope somebody will help me...

0
Comment
Question by:senico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 10867564
what code do you have so far?

You will need a StringTokenizer, a list of Strings, and a linelength in chars

Tim
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10867744
Where are you displaying the text? TextArea/ JTextArea/ JEditorPane/ JTextPane/ something else?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10867847
> then want to save this text in file .txt
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 7

Expert Comment

by:tomboshell
ID: 10868492
First it might help to understand the file types.  A text file (extension .txt) does not allow for a lot of formating.  Other programs do take formatting into account.  It is 'left-justified' by default just because the text is listed as it appears (nothing fancy).  Most just simply use liberal amounts of tab characters and empty spaces for formating.  If you are really dead set on having a formatted document then I would suggest to look into creating a rich-text document (rtf), or html or some such.  
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10868506
> A text file (extension .txt) does not allow for a lot of formating.

But you can assume it's a fixed-width charset, and format the text accordingly...
0
 
LVL 1

Author Comment

by:senico
ID: 10870569
Hello...

Actually i have to read the text from file .txt(example: input.txt). After that, I have to display the text in file .txt(example: ouput.txt). I want the text to be left and right justify format. So now, i want to know how to justify the text in file .txt .....here are the code...
+++++++++++++++++++++++++++++++++++++++++++
import java.io.*;

class bentuk
 {
      public static void main(String args[])
       {             
             System.out.println("Baca data dr fail\n");
             int count = 0;
             int bil = 0;
             int i=0;
             String kosong = "";
             try
              {
                    BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
              BufferedReader in = new BufferedReader(new FileReader("input.txt"));
              String str;
              
              System.out.println("Papar output\n");
              while ((str = in.readLine()) != null)   //read data from input file
               {
                
                       
                 System.out.println(str); //display data
                 //System.out.println(str.length()); //panjang data
                
                 out.write(str);  // write to output file
                 out.newLine(); //new line


               } //end while
           
               
              in.close();
              out.close();
         }
        catch (IOException e)
        {
       
        }

       } //end main
      
      
 } //end format
++++++++++++++++++++++++++++++++++++++++++
 
0
 
LVL 15

Accepted Solution

by:
JakobA earned 500 total points
ID: 10874246
The below is for use with fixed-width fonts only (courier)

read your text into an array of stings where each string represent a line <=  the desired nr of characters.
note the number of characters missing to have that line justified
note the number and position of all blanks in the line.
insert blanks after those blanks until the desired length of string is achieved. spread them out over the line, fx:

"the cow jumped over the moon"   24 characters, but we want 27 in each line
3 blanks are to be added
there are 5 blanks in the line at positions 4, 8, 11, 16, 20

so we want to add a blank to the  first, third and fifth space in the line.  Or rather to the fifth, third and the first (stringlength increase at each insertion shifting later positions). You may need to insert more than one space after some or all blanks to expand the line.

take note of the last line in each paragraph. it should not be justified

and watch out for special situations like a single word beeing so long that it is the only word on a line, if that happens just give up on justifying that line. cant be done.

--------------

If you are using a proportional font ( iiiiii < mmmmmm ) you are out of luck unless you can find some Java package that does the job for you. I would expect someone has made one, so it is worth a search.

regards JakobA
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10876494
>> > then want to save this text in file .txt

Ah, sorry, initially I assumed that you wanted to display it in a justified form in some Component. Well, for your case, you will probably need some special API for writing to RTFs or Word docs. Try: http://api.openoffice.org
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11676051
I'd say JakobA got it... :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question