Results 1 to 4 of 4
  1. #1
    egjava is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default String composing problem ...

    Hello Friends!

    I have problem with String composition. I want to compose a string with a '+' operator. But the result is not what I would expect.
    Here is the example: String date = "16" + "." + "11" + "." + " 2011";
    I expect to get a string date written in this form: "16.11.2011", but unfortunatelly the result is "16.112011" so the second dot (.) is missing.

    Of course, in real program I use variables intead of "16", "11" and "1911" but the problem is the same!

    So, where does the problem lie?

    I know that I need to write dot character '.' in this shape '\\." when I wish to search for dot character (.) in string or when I need to parse string with dot character. But I don't know for the reason above.

    Thank you for your help in advance,
    Ernest

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: String composing problem ...

    first of all you have to use StringBuilder or StringBuffer for concatenations string because an operation plus is so expensive for your performance.
    Java Code:
    String date = "16" + "." + "11" + "." + " 2011";
    This is correct code. I guess that you miss your dot in another parts your code. Can you show us your code?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    egjava is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: String composing problem ...

    My original code is bellow. If you use code inside comment /* */ with '-' instead of '.' everything is OK (16.11.2011), but with '.' I get 16.112011 (dot is missing).

    public void nastaviIzbranDatumZaPrisotnost(String imeTabelePrisotnost){ //v obliki 'prisotnost2011_10' to metodo kličemo iz
    // class JedPulPregledPrisotnostiPoOsebi->public void zgradiSQLQueryZaTiskanje(){
    System.out.println("Ime tabele = " + imeTabelePrisotnost);

    String temp1 = imeTabelePrisotnost.replaceFirst("prisotnost","");//iz prisotnost2011_10 dobim 2011_10
    String[] temp2 = temp1.split("_");

    String leto = temp2[0];
    String mesec = temp2[1];

    izbranoLeto = leto;
    izbranMesec = mesec;

    /*
    izbranDatumZaPrisotnost = "1-" + mesec;
    izbranDatumZaPrisotnost = izbranDatumZaPrisotnost + "-";
    izbranDatumZaPrisotnost = izbranDatumZaPrisotnost + leto;
    */


    izbranDatumZaPrisotnost = "1." + mesec;
    izbranDatumZaPrisotnost = izbranDatumZaPrisotnost + ".";
    izbranDatumZaPrisotnost = izbranDatumZaPrisotnost + leto;



    System.out.println("Date: " + izbranDatumZaPrisotnost);

    }

    Best regards,
    Ernest

  4. #4
    egjava is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: String composing problem ...

    Thank you very much for your help! It's working now. I'm sorry, it was my mistake. Later I realized that some code after this method changed the string.
    Best regards,
    Ernest

Similar Threads

  1. String Parsing Problem
    By jamess in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 10:59 AM
  2. Replies: 6
    Last Post: 12-06-2010, 10:26 AM
  3. string to negative int problem
    By pendulum101 in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 06:47 PM
  4. String Problem
    By gnomewise in forum New To Java
    Replies: 1
    Last Post: 10-19-2008, 01:37 PM
  5. problem with using string in if condition
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 11:40 PM

Posting Permissions

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