Solved

how to justify text

Posted on 2004-04-20
12
1,435 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
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:mayankeagle
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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 125 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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
sumHeights2  challenge 7 76
bunnyEars2 challenge 6 70
eclipse formatting 6 43
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now