Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2008, 10:09 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 237
willemjav is on a distinguished road
What´s in a string?
What´s in a string?

A string can be a single character, a word, or line of words. Strings might include line breaks:
String str = "This is the first string´s line \n" +
"plus this is the second string´s line \n" +
"etc ";
Writing a multi-line string to a file can be easily done by:

public void Writesourcedata(String pth) {
PrintWriter outputStream;
try {
outputStream = new PrintWriter (new FileWriter(pth));
outputStream.print(str);
}
outputStream.close();
etc.
The line breaks appear correctly inside the text.
But reading the file back into a application is troubling, the next
Code does put the string togther but ignores the linebreaks


private boolean readsourcedata(String path) { // the source file reader

try {
scanner = new Scanner( new FileReader(path) );
}
catch (FileNotFoundException e) {
System.out.println(path + " not present");
}
if (scanner==null) {
System.out.println(path + " can not read that file");
}
for ( i = 0; i < numberpic; i++) {
str = scanner.nextLine();
img[i].infotext = " ";
while (!str.equalsIgnoreCase("//*")) {
img[i].infotext = img[i].infotext.concat(" " + str);
str = scanner.next();
}
etc.
Each end of the string is marked by //* but the line breaks are not registered by this code. I tried
if (str.equalsIgnoreCase("\n"))
str = (str + " \n"); //(maybe a little silly to do)
but did not work. An other approach would be to read the whole line scanner.nextLine(); but than the problem would be, how to mark the end of the string block (//*).

Willemjav
(to string or to string)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008, 10:44 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 237
willemjav is on a distinguished road
to string or not to string
while (!str.equalsIgnoreCase(" //*")) {
img[i].infotext = img[i].infotext.concat(str + "\n");
str = scanner.nextLine();
}

The marker //* appears on each new line and by reading the string line by line the code actually works including the line breaks

to string or not to string

willemjav
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2008, 05:30 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 962
Norm is on a distinguished road
A string contains one or more Unicode characters. There are about 65K different values for a character.
Some java classes are sensitive to lineend characters and other formating characters.
Can't really see any question in this thread.

Why is this thread in a thread for Applets?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2008, 10:23 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 237
willemjav is on a distinguished road
Good question norm, I do not know myself, but you know it all started with an image-applet that already functions on my webpage (WILLEM DRAGSTRA WEB SITE). Some later I decided to develop a parent application that prepares the images for the applet and eventually will improve my applet too. I happen to meet some wonder full people (that help me polishing my code) at this applet-place and I am afraid not to find them at other spots?
The question is if each thread needs to have a question… some times I even answer my own questions myself… you might ask the sense of that as well…but anyway these monologues help me to keep on programming… so do not get upset Norm if things are not according the norms, because I do ask a lot of programming questions to (at this time I am working on a glass pane and questions will pop up).

So what’s in a thread anyway?
willemjav
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-01-2008, 11:31 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 962
Norm is on a distinguished road
I only read threads that I know something about or an interested in. If a question I could answer is posted in a thread I'm not interested in, I'll never see it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String one198 New To Java 1 01-07-2008 10:59 AM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 05:59 PM
Help with insertName(String name) and deleteName(String name) trill New To Java 1 08-07-2007 08:29 AM
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String) baltimore New To Java 1 08-06-2007 08:45 AM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 11:52 AM


All times are GMT +3. The time now is 12:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org