Results 1 to 20 of 22
Thread: Str.charAt() ?!
- 08-20-2011, 09:58 PM #1
- 08-20-2011, 10:07 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.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; }Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 08-20-2011, 11:11 PM #3
char is an integral type.
Types, Values, and Variables
db
- 08-22-2011, 06:17 AM #4
- 08-22-2011, 06:53 AM #5
Once again you exhibit your lack of understanding.
Which is perfectly fine.
Output is "hello".Java Code:String str = "hell"; char c = 'o'; str += c; System.out.println(str);
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.
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.Java Code:String str = "hel"; char c1 = 'l'; char c2 = 'o'; str += c1 + c2; System.out.println(str);
- 08-22-2011, 08:09 AM #6
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 08:18 AM #7
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.
Saying that someone MUST do something a particular way is misleading. There is usually various ways to achieve the same result.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);
- 08-22-2011, 08:28 AM #8
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.
See, this way everything is of the same type....Java Code:String str = "hel"; char c1 = 'l'; char c2 = 'o'; str += Character.toString( c1 ); str += Character.toString( c2 ); System.out.println(str);
Last edited by stchman; 08-22-2011 at 08:33 AM.
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 08:45 AM #9
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.
- 08-22-2011, 09:08 AM #10
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);
- 08-22-2011, 10:48 AM #11
- 08-22-2011, 12:09 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 08-22-2011, 01:42 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-22-2011, 07:33 PM #14
Hmmmmmm.
I don't see how people think this
Junky's code
Is more clear or correct than this: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);
I accomplished the SAME thing with LESS code. I gather to some that is a bad thing.Java Code:String str = "hel"; char c1 = 'l'; char c2 = 'o'; str += Character.toString( c1 ) + Character.toString( c2 ); System.out.println(str);
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 07:52 PM #15
Which method uses more computer resources?
String concatenation can be expensive.
- 08-22-2011, 08:03 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-22-2011, 08:11 PM #17
If Junky is, then how come his code is incorrect?
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).buffer.append(c1).buffer.append(c2).toString(); System.out.println(str);
I guess when anyone else posts bad code, it's OK.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);
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 08:23 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
[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.
The same counts for you: test your code before you claim non-quantitative qualifiers such as 'better'. Your latest conclusion is incorrect.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-22-2011, 08:46 PM #19
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-22-2011, 09:29 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Why won't charAt() work?
By MetalR0 in forum New To JavaReplies: 6Last Post: 08-04-2011, 09:46 AM -
charAt problem... Please help
By Matija in forum New To JavaReplies: 7Last Post: 01-21-2011, 01:20 AM -
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 06:57 PM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 05:27 PM -
Help With Input.charAt(LastIndex);
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:22 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks