Results 1 to 5 of 5
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default 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)

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default 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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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?

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  2. String
    By one198 in forum New To Java
    Replies: 1
    Last Post: 01-07-2008, 10:59 AM
  3. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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