Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed - hat this function does and identify any errors  RSS feed

 
radha MS
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am supposed to say what this function does and identify any errors.

public String mystery(String num) {
if (num == null) {
return "N/A";
}
int len = num.length();
int c = 0;
char[] sb = new char[len];
for (int i = 0; i < len; i++) {
sb[c++] = num.charAt(i);
if ((len - 1 - i) % 3 == 0 && i != len - 1) {
sb[c++] = ',';
}
}
return new String(sb);
}

I see there is an ArrayIndexOutOfBoundsException, but I'm stuck there. First it grabs len which is the length of the string "test" would be 4. I'm thinking this is supposed to take the string passed in and separate the characters with commas, so "test" would return "t,e,s,t". And I think the problem is with the incrementing of c.

Any help would be greatly appreciated.

TIA,
JS
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so "test" would return "t,e,s,t".

I think it would rather result in something like "t,est", splitting every three characters, from the back.
The problem is : char[] sb = new char[len];
As you are inserting commas, you will need more than "len" for your array. You have to compute the number of commas that will be added, and declare your array length as "len + numberOfCommas"
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by veena MS:
I am supposed to say what this function does and identify any errors.

public String mystery(String num) {
if (num == null) {
return "N/A";
}
int len = num.length();
int c = 0;
char[] sb = new char[len];
for (int i = 0; i < len; i++) {
sb[c++] = num.charAt(i);
if ((len - 1 - i) % 3 == 0 && i != len - 1) {
sb[c++] = ',';
}
}
return new String(sb);
}



see the code I have bolded. Your array is of length equals to length of String,now you also adding ',' in your char array so it can not accomodate all your chars and you get ArrayIndexOutOfboundException.

For example lets say your String num is "Bharat".

here length of string is 6 and so is you char array's. So when you add ',' there is no room for last char 't' and you get that exception.

BTW output with string "Bharat" is Bha,rat not B,h,a,r,a,t.

Following may help you


[ June 27, 2007: Message edited by: Bharat Makwana ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a question (like this one) looks like homework, it is better to provide hints to the solution rather than giving the answer. (please )
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"veena MS",

Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Regards,
Katrina Owen
Bartender
[ June 27, 2007: Message edited by: Katrina Owen ]
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
When a question (like this one) looks like homework, it is better to provide hints to the solution rather than giving the answer. (please )


Point is noted Mr.David,Thank you.


[ June 27, 2007: Message edited by: Bharat Makwana ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"radha MS"

Please review the naming policy again as your last name is still invalid. This is your second warning.

Scott Selikoff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!