Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree3Likes

Thread: Str.charAt() ?!

  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default Str.charAt() ?!

    why String.charat return number of char?!

    like this code :
    if(a.length()==b.length()){
    for(int i=0;i<a.length();i++){
    res += a.charAt(i) + b.charAt(i);
    }
    return res;
    }


    =========================
    return this : "217219221"
    instead of this >> mixString("abc", "xyz") → "axbycz"

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Java Code:
    String res;
    if(a.length()==b.length()){
    for(int i=0;i<a.length();i++){
    res += a.charAt(i) +""+ b.charAt(i);
    }
    return res;
    }
    I assume that when you do a.charAt() + b.charAt() you actually adding decimal representations of ascii characters. Maybe if you add string the whole thing should be represented as a String, so the a.charAt() + b.charAt() wont be added together.
    HearT.Hunt3r likes this.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    char is an integral type.
    Types, Values, and Variables

    db

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by HearT.Hunt3r View Post
    why String.charat return number of char?!

    like this code :
    if(a.length()==b.length()){
    for(int i=0;i<a.length();i++){
    res += a.charAt(i) + b.charAt(i);
    }
    return res;
    }


    =========================
    return this : "217219221"
    instead of this >> mixString("abc", "xyz") → "axbycz"
    Assumption - res is of type String.

    You are attempting to concatenate a char to a String. You will need to convert those char to String. Check out the Character class.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Once again you exhibit your lack of understanding.

    Quote Originally Posted by stchman View Post
    You are attempting to concatenate a char to a String.
    Which is perfectly fine.
    Java Code:
    String str = "hell";
    char c = 'o';
    str += c;
    System.out.println(str);
    Output is "hello".

    However the problem the OP is facing is that they have 2 chars on the righthand side and their ascii values are added together instead of the two chars being concatenated together. The result of the addition is then concatenated to the String.
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    str += c1 + c2;
    System.out.println(str);
    Ouput is "hel219" because 'l' has a value of 108 and 'o' has the value of 111. 108 + 111 = 219 which is concatenated to the end of the String.
    HearT.Hunt3r likes this.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Once again you exhibit your lack of understanding.


    Which is perfectly fine.
    Java Code:
    String str = "hell";
    char c = 'o';
    str += c;
    System.out.println(str);
    Output is "hello".

    However the problem the OP is facing is that they have 2 chars on the righthand side and their ascii values are added together instead of the two chars being concatenated together. The result of the addition is then concatenated to the String.
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    str += c1 + c2;
    System.out.println(str);
    Ouput is "hel219" because 'l' has a value of 108 and 'o' has the value of 111. 108 + 111 = 219 which is concatenated to the end of the String.
    Once again, for someone who is ignoring me you are doing a really bad job.

    I maintain that the OP is trying to create a string from a series of chars.

    In your opinion, what is the OP trying to do?
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I explained in another post why I need to keep my eye on you, to ammend all the incorrect replies you make.

    I undeerstand what the OP is trying to do. What I have an issue with is you proclaiming that they need to convert the chars to Strings first. To correct the "bad" code above I would use a StringBuilder.

    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    StringBuilder buffer = new StringBuilder();
    buffer.append(str);
    buffer.append(c1);
    buffer.append(c2);
    System.out.println(buffer);
    
    // or
    
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    str += c1;
    str += c2;
    System.out.println(str);
    Saying that someone MUST do something a particular way is misleading. There is usually various ways to achieve the same result.
    killer8889 likes this.

  8. #8
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    I explained in another post why I need to keep my eye on you, to ammend all the incorrect replies you make.
    So you are now the java-forums.org police?

    Concatenating a char to a String is bad form. The proper way to do it is to convert the char to a String. If you wish to engage in sloppy programming, that is your wish.

    I would do it this way.

    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    str += Character.toString( c1 );
    str += Character.toString( c2 );
    System.out.println(str);
    See, this way everything is of the same type....
    Last edited by stchman; 08-22-2011 at 09:33 AM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by stchman View Post
    Concatenating a char to a String is bad form. The proper way to do it is to convert the char to a String.
    Yet again your lack of understanding shows.

    When you concatenate anything to a String what happens under the hood is that the code is changed to use a StringBuilder and amends the content regardless of its type. So converting the char to a String beforehand is actually causing the code to do extra work.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    By the way I realised the code I posted above can be improved somewhat (depending upon your point of view).
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    StringBuilder buffer = new StringBuilder();
    str = buffer.append(str).buffer.append(c1).buffer.append(c2).toString();
    System.out.println(str);

  11. #11
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Yet again your lack of understanding shows.

    When you concatenate anything to a String what happens under the hood is that the code is changed to use a StringBuilder and amends the content regardless of its type. So converting the char to a String beforehand is actually causing the code to do extra work.
    Let's agree that neither of us is going to change the other's mind. Just go ahead and ignore me again, at best you are annoying.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Quote Originally Posted by stchman View Post
    Concatenating a char to a String is bad form. The proper way to do it is to convert the char to a String. If you wish to engage in sloppy programming, that is your wish.
    I'm afraid Junky's correct.
    The standard way to do it is to use the StringBuilder, since one is created under the hood anyway. Using the Character.toString() you're adding an unecessary step.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stchman View Post
    Once again, for someone who is ignoring me you are doing a really bad job.
    Junky is doing a fine job; if he didn't do this, I or another moderator had to do this because in general your 'advice' (mind the quotes) is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Hmmmmmm.

    I don't see how people think this

    Junky's code
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    StringBuilder buffer = new StringBuilder();
    str = buffer.append(str).buffer.append(c1).buffer.append(c2).toString();
    System.out.println(str);
    Is more clear or correct than this:
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    str += Character.toString( c1 ) + Character.toString( c2 );
    System.out.println(str);
    I accomplished the SAME thing with LESS code. I gather to some that is a bad thing.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

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

    Default

    Which method uses more computer resources?
    String concatenation can be expensive.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stchman View Post
    Hmmmmmm.

    I accomplished the SAME thing with LESS code. I gather to some that is a bad thing.
    Run javap -c on your class and see for yourself what the compiler has generated for you behind your back; please stop being stubborn and please stop spreading bad advice.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Junky is doing a fine job; if he didn't do this, I or another moderator had to do this because in general your 'advice' (mind the quotes) is incorrect.

    kind regards,

    Jos
    If Junky is, then how come his code is incorrect?

    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    StringBuilder buffer = new StringBuilder();
    str = buffer.append(str).buffer.append(c1).buffer.append(c2).toString();
    System.out.println(str);
    When the correct code should be:
    Java Code:
    String str = "hel";
    char c1 = 'l';
    char c2 = 'o';
    StringBuilder buffer = new StringBuilder();
    str = buffer.append(str).append(c1).append(c2).toString();
    System.out.println(str);
    I guess when anyone else posts bad code, it's OK.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    [QUOTE=stchman;230477]If Junky is, then how come his code is incorrect?

    It may be a slip of the finger but at least I got the intention of the code; but you are right: the code should've been tested before being posted.

    Quote Originally Posted by stchman View Post
    I guess when anyone else posts bad code, it's OK.
    The same counts for you: test your code before you claim non-quantitative qualifiers such as 'better'. Your latest conclusion is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    [QUOTE=JosAH;230478]
    Quote Originally Posted by stchman View Post
    If Junky is, then how come his code is incorrect?

    It may be a slip of the finger but at least I got the intention of the code; but you are right: the code should've been tested before being posted.



    The same counts for you: test your code before you claim non-quantitative qualifiers such as 'better'. Your latest conclusion is incorrect.

    kind regards,

    Jos
    Fair enough. I bring up Geany, write a small program, then compile and debug the methods I've wrote. They may or may not be the most compiler "efficient", but they are easily readable.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stchman View Post
    Fair enough. I bring up Geany, write a small program, then compile and debug the methods I've wrote. They may or may not be the most compiler "efficient", but they are easily readable.
    That isn't a quantifiable statement either. case closed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Why won't charAt() work?
    By MetalR0 in forum New To Java
    Replies: 6
    Last Post: 08-04-2011, 10:46 AM
  2. charAt problem... Please help
    By Matija in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:20 AM
  3. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 PM
  4. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 06:27 PM
  5. Help With Input.charAt(LastIndex);
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:22 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
  •