GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2009, 00:20
Anonymous_guy Anonymous_guy is offline
New Member
 
Join Date: Nov 2007
Location: Victoria
Posts: 9
Anonymous_guy is on a distinguished road

Simple Text Indenter, Help.


I'm writing a simple text indenter for an assignment. The point of it is to enter in command line arguments that will provide an input file, output file, max characters per line and amount of spaced to indent by. It's pretty simple in the sense that it just adds spaces to the left of the line, and wrapping the words if it exceeds the maximum character per line, which is given by the user.

For example, the file:
Lets Test This with a width of 10 and a indent of 17 should output:
(10spaces)Lets
(10spaces)Test
(10spaces)This

Currently my output looks like:
(10spaces)lets
(10spaces)tes
(10spaces)t this

And as I add more words, it gets slightly more askew. The function following is what alters the input string. The input string contains the entire input file, spaces is a string that contains the number of spaces equal to the indent the user specified. Width is the maximum characters per line:
JAVA Code:
public static String format(String input, String spaces, int width)
    {
        int wSpace = 0;
        int counter = 1;
        int placement = 0;
        StringBuffer tmpBuffer = new StringBuffer(input);
        width = width - spaces.length();

        while(width*counter < input.length())
            {
                for(int j = wSpace; j != width*counter; j++)
                {
                   if(input.charAt(j) == ' ')
                   {
                       wSpace = j;
                   }
                }
                   tmpBuffer.insert(placement, spaces);
                   tmpBuffer.insert(wSpace+spaces.length()*counter, System.getProperty("line.separator"));
                   if(tmpBuffer.charAt(wSpace+spaces.length()*counter+2) == ' ' )
                   {
                       tmpBuffer.deleteCharAt(wSpace+spaces.length()*counter+2);
                       
                   }
                   placement = wSpace+spaces.length()*counter + 2;

  
                   counter++;
            }
                  
       tmpBuffer.insert(placement, spaces);
       input = tmpBuffer.toString();
      return input;
    }
  #2  
Old 04-Jun-2009, 06:55
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Simple Text Indenter, Help.


I'm confused as to the difference of 'width' and 'indent' in the original information... If you have a string of spaces, why the width?

Anyway, here's another way to work with the input and spaces without using 'width'.
JAVA Code:
public static String format(String input, String spaces, int width)
{
    /* Note that 'width' is NOT used here. */
    int placement = 0;
    StringBuffer tmpBuffer = new StringBuffer(input);
    String replaceText = new String( System.getProperty("line.separator") + spaces );

    /* Find the first space. */
    placement = tmpBuffer.indexOf(" ");

    while(  placement > 0 )
    {
        tmpBuffer.deleteCharAt(placement);
        tmpBuffer.insert( placement, replaceText );

        /* Any other spaces? [beyond what was just inserted] */
        placement = tmpBuffer.indexOf(" ", placement + replaceText.length() );
    }

    placement = 0; // set zero to insert at beginning.
    tmpBuffer.insert(placement, spaces);
    input = tmpBuffer.toString();

    return input;
}
Output from the example:
This was called using:
JAVA Code:
  
format( "Lets Test This", "          ", 0);
// the 2nd argument is a 10-space string.
Code:
Lets Test This Press any key to continue . . .

Note, though, that my example ASSUMES that the input string only has single-spacing, and does not account for multiple spaces, nor leading spaces, nor trailing spaces -- so having these situations would be extra handling that will need consideration.

HTH
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 04-Jun-2009 at 07:56.
  #3  
Old 04-Jun-2009, 21:41
Anonymous_guy Anonymous_guy is offline
New Member
 
Join Date: Nov 2007
Location: Victoria
Posts: 9
Anonymous_guy is on a distinguished road

Re: Simple Text Indenter, Help.


I guess my explanation was off. Width should be the maximum amount of characters in a line including the magnitude of spaces. For example say the indent (or in this function spaces.length()) is 5 and width is 20, then each line can only have a maximum of 15 characters with 5 spaces preceding them. So the point of my wSpace was to search through and find the last space that was less then width so I could wrap the words. Hope this makes it a bit more clear?


Edit: I was able to solve it. For the longest time I had:
JAVA Code:
wSpace++;
instead of:
JAVA Code:
wSpace = i;
That pretty much changed it for me and I was able to finish it. Thanks for your help tho Greatly appreciated.
  #4  
Old 10-Nov-2009, 23:18
kyo kyo is offline
New Member
 
Join Date: Nov 2009
Posts: 1
kyo is on a distinguished road

Re: Simple Text Indenter, Help.


then for mine , how to read from the source file to write to a output file by 10 character per line , and next line 10 character and so on..

JAVA Code:
public static void main(String[] args) throws IOException {       
        
        try {
          
fileIn = new FileReader(inputFile);
inn = new LineNumberReader(fileIn,10);
while ((line=inn.readLine()) != null) {
System.out.println("the number "+inn.getLineNumber() + ". " + line);
storeline[0]=inn.getLineNumber();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
     

        in = new FileInputStream(inputFile);
         out = new FileOutputStream(outputFile);
      

        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
        


    }
Last edited by admin : 11-Nov-2009 at 04:32. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 60 20-Aug-2009 05:04
A program that calculates the number of characters in the text file et21zero Java Forum 5 28-Apr-2008 12:04
Simple text file I/O question brookeville C++ Forum 7 08-Mar-2005 13:19
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:20.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.