Forums Register Login

help needed - hat this function does and identify any errors

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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"
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
When a question (like this one) looks like homework, it is better to provide hints to the solution rather than giving the answer. (please )
+Pie Number of slices to send: Send
"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 ]
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
Thanks
+Pie Number of slices to send: Send
"radha MS"

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

Scott Selikoff
It's exactly the same and completely different as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 926 times.
Similar Threads
int length(), char charAt(int index).HElp~~
what this function does and identify any errors
My Array
Recursion
Power Strings
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:40:14.