Solved

how to justify text

Posted on 2004-04-20
12
1,442 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: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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
Java SE 8u111  Lot of stuff broke 11 69
servlet filter example 37 61
expectj telnet failing 5 36
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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