Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to determine line break in textarea?

Reply
Thread Tools

How to determine line break in textarea?

 
 
Perfect Reign
Guest
Posts: n/a
 
      04-26-2005
I'm writing an application which has a text area in one of the classes.

I'm saving the information from the text area to a flat file - cvs
formatted. (Don't ask why, it just is so.) Anyway, I need to capture the
line breaks so that I can put a \n in my flat file for reloading the text
into the text area with the line breaks and so my flat file doesn't have
line breaks.

I'm thinking there's got to be some way of searching using firstindexof or
something like that. However, I can't find how to search for a line break
or carriage return (ASCII 10 or 13.)

I'm thinking I'd have to do something like:


String textarea = textComments.getText();
int index = textarea.firstIndexOf( CHR(10) );


Then I'd split the text and create a new string with the \n character.

--
kai - www.perfectreign.com

kai@yoda:~> format a:
Error: The DOS concept of formatting disk media is screwed.


 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      04-26-2005
In article <(E-Mail Removed)>,
Perfect Reign <(E-Mail Removed)> wrote:

> I'm writing an application which has a text area in one of the classes.
>
> I'm saving the information from the text area to a flat file - cvs
> formatted. (Don't ask why, it just is so.) Anyway, I need to capture the
> line breaks so that I can put a \n in my flat file for reloading the text
> into the text area with the line breaks and so my flat file doesn't have
> line breaks.
>
> I'm thinking there's got to be some way of searching using firstindexof or
> something like that. However, I can't find how to search for a line break
> or carriage return (ASCII 10 or 13.)
>
> I'm thinking I'd have to do something like:
>
>
> String textarea = textComments.getText();
> int index = textarea.firstIndexOf( CHR(10) );
>
>
> Then I'd split the text and create a new string with the \n character.


By "text area", do you mean JTextArea? If so then perhaps you should
revisit the API JavaDocs, where you'll see methods for obtaining the
information you want. You can get number of lines contained. You can
call getLineStartOffset(int line) to obtain the actual offset into the
complete contents for each line by number. If that doesn't provide the
information you want, then I must not correctly understand your question.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
 
 
 
Perfect Reign
Guest
Posts: n/a
 
      04-26-2005
On Tue, 26 Apr 2005 08:35:26 -0500, someone posing as Steve W. Jackson
donned fireproof bloomers and chiseled in the wall:

> In article <(E-Mail Removed)>,
> Perfect Reign <(E-Mail Removed)> wrote:
>
>> I'm writing an application which has a text area in one of the classes.
>>
>> I'm saving the information from the text area to a flat file - cvs
>> formatted. (Don't ask why, it just is so.) Anyway, I need to capture the
>> line breaks so that I can put a \n in my flat file for reloading the text
>> into the text area with the line breaks and so my flat file doesn't have
>> line breaks.
>>
>> I'm thinking there's got to be some way of searching using firstindexof or
>> something like that. However, I can't find how to search for a line break
>> or carriage return (ASCII 10 or 13.)
>>
>> I'm thinking I'd have to do something like:
>>
>> String textarea = textComments.getText();
>> int index = textarea.firstIndexOf( CHR(10) );
>>
>> Then I'd split the text and create a new string with the \n character.

>
> By "text area", do you mean JTextArea? If so then perhaps you should
> revisit the API JavaDocs, where you'll see methods for obtaining the
> information you want. You can get number of lines contained. You can
> call getLineStartOffset(int line) to obtain the actual offset into the
> complete contents for each line by number. If that doesn't provide the
> information you want, then I must not correctly understand your question.
>
> = Steve =


Yes, JTextArea. I suppose I should have googled that term.

However, getLineStartOffset(int line) give me, "Determines the offset of
the start of the given line," which is about as helpful as anything else in
the API. (Whoever wrote the API has a sick sense of humor because I spend
hours lookinga and get very little useful information. Kind of reminds me
of MSDN.)


Having thought a bit more on this, I'd actually like to replace the
carriage return CHR(13) with a newline character \n. Maybe that'll work.

I'll have to look this up. Something like: replace(mystring, Chr(13), "\n")

--
kai - http://www.velocityreviews.com/forums/(E-Mail Removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
Reply With Quote
 
Daniel Rohe
Guest
Posts: n/a
 
      04-26-2005
Do you know the MVC pattern? The JTextArea contains a document and a view.
The document contains the text that is displayed in the view. You can
retrieve the document by getDocument(). This method returns the default
implementation for JTextArea which is a PlainDocument. This stores the text
as a map of lines. But you can also call getText() on the document and get
the text as String. Normally the text has line breaks ("\n") and carriage
returns ("\r") when the user enters the text. But you can then use the
replace method of the text to replace occurences with something you want or
only the empty string. For the map of lines you can call
getDefaultRootElement(). This is the root element and contains a child
element for each line of text. On the root element you can call
getElementCount() to get the number of lines and you can call
getElement(index) to get the specific line. Each element has also a start
and end offset with which you can retrieve the text of the line from the
document.

Daniel

"Perfect Reign" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Tue, 26 Apr 2005 08:35:26 -0500, someone posing as Steve W. Jackson
> donned fireproof bloomers and chiseled in the wall:
>
>> In article <(E-Mail Removed)>,
>> Perfect Reign <(E-Mail Removed)> wrote:
>>
>>> I'm writing an application which has a text area in one of the classes.
>>>
>>> I'm saving the information from the text area to a flat file - cvs
>>> formatted. (Don't ask why, it just is so.) Anyway, I need to capture
>>> the
>>> line breaks so that I can put a \n in my flat file for reloading the
>>> text
>>> into the text area with the line breaks and so my flat file doesn't have
>>> line breaks.


Hm. Here you say you will put a "\n" in the flat file but short after that
you say your flat file has no line breaks?

....


 
Reply With Quote
 
Perfect Reign
Guest
Posts: n/a
 
      04-26-2005
On Tue, 26 Apr 2005 16:40:20 +0200, someone posing as Daniel Rohe donned
fireproof bloomers and chiseled in the wall:

> Do you know the MVC pattern? The JTextArea contains a document and a view.
> The document contains the text that is displayed in the view. You can
> retrieve the document by getDocument(). This method returns the default
> implementation for JTextArea which is a PlainDocument. This stores the text
> as a map of lines. But you can also call getText() on the document and get
> the text as String. Normally the text has line breaks ("\n") and carriage
> returns ("\r") when the user enters the text. But you can then use the
> replace method of the text to replace occurences with something you want or
> only the empty string. For the map of lines you can call
> getDefaultRootElement(). This is the root element and contains a child
> element for each line of text. On the root element you can call
> getElementCount() to get the number of lines and you can call
> getElement(index) to get the specific line. Each element has also a start
> and end offset with which you can retrieve the text of the line from the
> document.
>
> Daniel


Cool. I'll try and digest this.


>>> In article <(E-Mail Removed)>,
>>> Perfect Reign <(E-Mail Removed)> wrote:
>>>
>>>> I'm writing an application which has a text area in one of the classes.
>>>>
>>>> I'm saving the information from the text area to a flat file - cvs
>>>> formatted. (Don't ask why, it just is so.) Anyway, I need to capture
>>>> the
>>>> line breaks so that I can put a \n in my flat file for reloading the
>>>> text
>>>> into the text area with the line breaks and so my flat file doesn't have
>>>> line breaks.

>
> Hm. Here you say you will put a "\n" in the flat file but short after that
> you say your flat file has no line breaks?
>



Right.

User enters:

Line One.
Line Two.
Third Line.


I will store:

'Line One.\nLine Two.\nThird Line.'


--
kai - (E-Mail Removed) - www.perfectreign.com

kai:/> format a:
Error: The DOS concept of formatting disk media is screwed.
To format a floppy, use "fdformat /dev/fd0"
and then "mkfs.minix /dev/fd0".
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-26-2005
Perfect Reign wrote:
> Having thought a bit more on this, I'd actually like to replace the
> carriage return CHR(13) with a newline character \n. Maybe that'll work.


If you managed to have carriage returns in the textarea text then you
already screwed up things. Java's internal line separator is '\n'
(newline) only, and nothing else (not to be confused with the
line.separator property, which indicates the operating-system's line
separator, not Java's internal line separator).

> I'll have to look this up. Something like: replace(mystring, Chr(13), "\n")


Well, I think you want to have a look at Java's basic syntax. Not that
you need it for the text area problem, but just so you know it in the
future.

Regarding the problem to get the text data line by line, there are
several ways:

The cleanest way would be to get the Document, which should be a
PlainDocument for a JTextArea. Then you could iterate over the Elements
(the Elements of a PlainDocument are the lines). The start/end offsets
for each Element can be used to obtain the corresponding line with the
Document's getText() method.

All this is rather dumb stuff. It makes sense to use this method if you
have a large document, and want to somewhat minimize copying text data
around.

A much simpler way is to get the whole text and split it:

String lines[] = textarea.getText().split("\\n");
for(int i = 0; i < lines.length; i++) {

}

If you don't want the intermediate array, you can burn a few more CPU
cycles and use Readers or better yet the old StringTokenizer.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      04-26-2005
In article <(E-Mail Removed)>,
Perfect Reign <(E-Mail Removed)> wrote:

> On Tue, 26 Apr 2005 08:35:26 -0500, someone posing as Steve W. Jackson
> donned fireproof bloomers and chiseled in the wall:
>
> > In article <(E-Mail Removed)>,
> > Perfect Reign <(E-Mail Removed)> wrote:
> >

[ snip ]
> >
> > By "text area", do you mean JTextArea? If so then perhaps you should
> > revisit the API JavaDocs, where you'll see methods for obtaining the
> > information you want. You can get number of lines contained. You can
> > call getLineStartOffset(int line) to obtain the actual offset into the
> > complete contents for each line by number. If that doesn't provide the
> > information you want, then I must not correctly understand your question.
> >
> > = Steve =

>
> Yes, JTextArea. I suppose I should have googled that term.
>
> However, getLineStartOffset(int line) give me, "Determines the offset of
> the start of the given line," which is about as helpful as anything else in
> the API. (Whoever wrote the API has a sick sense of humor because I spend
> hours lookinga and get very little useful information. Kind of reminds me
> of MSDN.)
>
>
> Having thought a bit more on this, I'd actually like to replace the
> carriage return CHR(13) with a newline character \n. Maybe that'll work.
>
> I'll have to look this up. Something like: replace(mystring, Chr(13), "\n")


There's simply no need to do that. First off, don't forget that the
specific end of line sequence varies by platform. But it's very simple
to do it just as I suggested.

You want to know how many lines exist based on when the user added hard
returns. And the last line, which may well not have a hard return, is
still a line. So you call the JTextArea's getLineCount() method. Then,
loop from zero to that value minus one and call getLineStartOffset(int
line) to find out where that line begins, followed by
getLineEndOffset(int line) to find out where it ends. You've already
got access to the text in the inherited getText() methods, one of which
takes a starting offset and length. Or you can use the Document, as
someone else has suggested, which also contains a getText() method
taking two int values.

This is better than searching for any specific character in that it's
not specific to any platform or character. And it's not difficult to do.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      04-26-2005
"Perfect Reign" <(E-Mail Removed)> schreef in bericht
news(E-Mail Removed)...
> I'm writing an application which has a text area in one of the classes.
>
> I'm saving the information from the text area to a flat file - cvs
> formatted. (Don't ask why, it just is so.) Anyway, I need to capture the
> line breaks so that I can put a \n in my flat file for reloading the text
> into the text area with the line breaks and so my flat file doesn't have
> line breaks.


What goes wrong when you dump the string from the textarea into the file? If
they both have line breaks, all should be OK!


 
Reply With Quote
 
Betty
Guest
Posts: n/a
 
      04-27-2005

"Boudewijn Dijkstra" <(E-Mail Removed)> wrote in message
news:d4mg78$obu$(E-Mail Removed)...
> "Perfect Reign" <(E-Mail Removed)> schreef in bericht
> news(E-Mail Removed)...
> > I'm writing an application which has a text area in one of the classes.
> >
> > I'm saving the information from the text area to a flat file - cvs
> > formatted. (Don't ask why, it just is so.) Anyway, I need to capture

the
> > line breaks so that I can put a \n in my flat file for reloading the

text
> > into the text area with the line breaks and so my flat file doesn't have
> > line breaks.

>
> What goes wrong when you dump the string from the textarea into the file?

If
> they both have line breaks, all should be OK!
>


I have windows XP Pro, sp1

I have a JTextArea and I save the contents when the window closes via

fos = new FileOutputStream("textfile.txt");
fos.write(DynamicPanel.dynamicOutArea.getText().ge tBytes());

What I find from a hex dump is that there is a single x0a char
at the end of each line. So the command
type textfile.txt
looks fine, but in the editor
notepad textfile.txt
it is a long single line (dos needs \r\n)

HTH



 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-27-2005
Boudewijn Dijkstra wrote:
> What goes wrong when you dump the string from the textarea into the file? If
> they both have line breaks, all should be OK!


Nothing would be OK if the operating system does not use a single '\n'
for EOL. You have to convert Java's EOLs (single '\n's) to whatever the
OS uses as EOL (which can be found in the "line.separator" System property).

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
How to determine if a line of python code is a continuation of the line above it Sandra-24 Python 5 04-09-2006 08:28 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments