Two Laptop Bag*
The moose likes I/O and Streams and the fly likes [solved]:p how to keep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "[solved]:p how to keep "next line" during append String data to StringBuilder" Watch "[solved]:p how to keep "next line" during append String data to StringBuilder" New topic
Author

[solved]:p how to keep "next line" during append String data to StringBuilder

L Willie
Ranch Hand

Joined: Aug 10, 2008
Posts: 51
how to keep the whole retrieve data and print outside the loop

joe,me again
Since the final result of 'display' can not keep the whole data that retrieved from Memorandum.txt, I can't return the display data.
How to keep the whole data?is it should use array or list?

Following is my array method,however can't get result i expected



[ October 11, 2008: Message edited by: L Willie ]
[ October 11, 2008: Message edited by: L Willie ]
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Sorry for not being Joe

If I got your question right, you want to read a text file completely into memory and afterwards do something with the Strings. Is that correct?
If you want to store single lines, I'd opt for a List<String>. If you you use an ArrayList for example, you don't have to care about the number of lines to read beforehand as you would have to if you would use plain arrays (that is in fact the reason why your solution fails).
Another possibility is to use the StringBuilder class, which is designed for storing character sequences effectively. The advantage is that you are able to manipulate the characters/strings afterwards or perform regular expressions on the complete text.

One last thing:

This loop is perfectly legal and will work properly. However, the Scanner class has appropriate hasXXX() methods for all nextXXX() calls. I would use the same XXX in such a loop to avoid any confusion or broken code.

Regards,
Thomas


SCJP 5.0, SCJD in progress
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Remember that nextLine() removes your line breaks. Therefore, if using a StringBuilder, don't forget to add those yourself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
L Willie
Ranch Hand

Joined: Aug 10, 2008
Posts: 51
thank Thomas and Rob


this is my solution base on both reply.
Now the finalDisplay can print whole retrieve data.However,the data "crowd" in one line without break to next line.

following is the result of 'finalDisplay'
MemorandumTo: Mrs. RemaFrom:Lou Willie WEK050051Date: 21 September 2008Subject:Seldom student use locker of Faculty of Computer Science & Information Technology (FCSIT)Since the process to apply the key of the locker that provides by faculty is not clear due to lack of clear guidance to student. Moreover there have alternate cabinet which no application require, it is quite convenience to our student. Although the cabinet is not secure cause these cabinet is �open air� to public. Hence student only put worthless things to the cabinet.WORK COMPLETEDWe are making a few things and the work has been done. As shown below is the outlines of our work completed.Finding group membersFirstly, we must find group members of three people before start our project. BrainstormingThis process is important because we discuss about our project problem statement, backgroundInformation, possible causes or reason, possible consequences and data gathering method .Besides, also discuss and divide particular work to do of each of the group member.SurveySurvey in an important step we get information we need. The survey form consist the question that ask the respondent or student about their opinion about locker that provide by faculty. The survey form will be collected and then the result of each respondent�s answer will be recorded and the data will be calculated and make statistic comparison.ObservationObservation is another method that we are get information about use of locker. In the observation, almost no students use the locker.WORK SCHEDULEDThe primary responsibilities remaining are writing recommendation report. This report is to recommend to our Dean of FSCIT that our suggestions replacing current locker to coin locker.


how to keep the nextline during append data?
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Well that's exactly what Rob suggested. The nextLine() methods removed the line breaks from the strings. Therefore, you have to add it yourself:

This would be the simple solution. However, different OS handle line breaks differently. I think, there is a system property giving you the right one for your system.

Regards,
Thomas
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
And here it is:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [solved]:p how to keep "next line" during append String data to StringBuilder