Results 1 to 13 of 13
  1. #1
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default How to left justify and wrap text

    Hello,

    I have a text file with 3 lines of text. I want to wrap text and left align it.
    As I am new to programming I have no idea from where to begin. Please need your help guys

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to left justify and wrap text

    Quote Originally Posted by Tejas View Post
    Hello,

    I have a text file with 3 lines of text. I want to wrap text and left align it.
    As I am new to programming I have no idea from where to begin. Please need your help guys
    Start by reading your input word by word. Can you do that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    Right now i have one line of code String[] paragraphs = FileUtil.readFile("test.txt") which reads the whole file to an array of strings

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to left justify and wrap text

    What does "left justify and wrap text" mean? We don't know the context of your question.

    Are you displaying the text in a GUI?

    Are you going to print the text on a bill or some other type of document.

    Are you doing something else with the text.

  5. #5
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    I have to write a program to take a piece of text from the text file ( such as maximum of 80 characters per line ) and left justify it using column length. For this I am told to use length of an array. I am using Eclipse IDE and need to display the result in the output window.

  6. #6
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    I have managed to write this piece of program, but the loop part doesnt seem to work. Please help

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int count = 0;
    String[] paragraphs = FileUtil.readFile("test.txt");
    String[] aread;
    aread = new String[80];

    for(String item : paragraphs)
    {
    String r = item.substring(0,190);

    for(int i = 0; i < r.length(); i++)
    {
    System.out.println(r);
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    The output is printing the same line again and again. I want it to display the next set of 190 characters onto next line and so on.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to left justify and wrap text

    Quote Originally Posted by Tejas View Post
    The output is printing the same line again and again. I want it to display the next set of 190 characters onto next line and so on.
    Here:
    Java Code:
    for(int i = 0; i < r.length(); i++)
    {
       System.out.println(r);
    }
    You're telling Java to print the r String multiple times, as many times as there are characters in the r String, so the program is only doing what you tell it to. I'm not sure why you have this bit of code in there. In fact most of your code confuses me, sorry. Perhaps you could tell us what it is doing at various spots. Also, when posting code here, you'll want to place the tag [code] just above your code block and the tag [/code] just below your code block so that your code retains its formatting and is readable.

  10. #10
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    Ok, I will explain it in simple terms, Can you tell me how to write a program to display text which is in a notepad file, in wrapped format :

    For example the text is : Google said Microsoft was resorting to "the same tactic we've seen time and again".It added: "We remain focused on building new technology and supporting Android partners."Per Roman of technology investment bank GP Bullhound said he was not surprised by the Samsung-Microsoft announcement. "Many people have long said that Android contains some Microsoft technology," he said. "Ultimately we are in the area of IP [intellectual property] wars. There is now an intense battle among the technology giants regarding their IP portfolios."

    And I want the output to look like this :

    Google said Microsoft was resorting to "the same tactic we've seen time and again". It added: "We remain focused on building
    new technology and supporting Android partners."
    Per Roman of technology investment bank GP Bullhound said he was not surprised by the Samsung-Microsoft announcement.
    "Many people have long said that Android contains some Microsoft technology," he said.
    "Ultimately we are in the area of IP [intellectual property] wars. There is now an intense battle among the technology giants
    regarding their IP portfolios."

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to left justify and wrap text

    Myself, I prefer not to "tell you how to write...", I'd much rather work with your own code and try to help you get it to work. If you're OK with this, then could you address the points in my previous post?

  12. #12
    Tejas is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to left justify and wrap text

    import java.io.*;

    public class Justification {


    public static void main(String[] args) {


    String[] paragraphs = FileUtil.readFile("test.txt"); // Reads a text file which is unwrapped and not justified. FileUtil is class which is already created and stores the text in the paragraphs array.


    for(String item : paragraphs) //the for loop displays the text stored in paragraph array in the eclipse console
    {
    String r = item.substring(0,80); // As the text is unwrapped I am trying to select 80 characters from the first line and then trying to put " \n " to place the next next 80 characters below it and so on

    for(int i = 0; i < r.length(); i++)
    {
    System.out.println(r); // But this part only displays the first 80 characters again and again from the text file
    }


    }

    }
    }

  13. #13
    utilman is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Re: How to left justify and wrap text

    Hmm this seems interesting. I am not equipped to reply, but I'll watch it.

Similar Threads

  1. How to wrap text in a JList
    By applewood13 in forum New To Java
    Replies: 4
    Last Post: 05-11-2011, 03:48 PM
  2. word wrap text in rows of Jtable
    By pink123 in forum AWT / Swing
    Replies: 3
    Last Post: 03-10-2011, 08:20 PM
  3. Replies: 4
    Last Post: 02-05-2011, 07:47 PM
  4. Replies: 0
    Last Post: 09-28-2010, 10:52 AM
  5. Replies: 2
    Last Post: 08-08-2010, 09:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •