If you know Java Programming please read

Status
Not open for further replies.

yourbeliefs

Something Profound
Original poster
Pub Member / Supporter
Sep 20, 2007
13,170
276
Northeast
I'm stuck with this homework thing trying to figure out what this java code is supposed to do and how to fix it. I've been working on this forever. I know programming but I'm not good with Java, and also it doesn't help that this code is not written correctly:

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 know this may not be the best place to post this, but I'm not a part of any other forums really, nor do I know anyone who knows Java (with the exception of my sister's ex husband :mad:)
Any help is greatly appreciated. Thank you.
 
its not that bad.. first part just makes sure the value passed in itn't null.. if it is returns N/A..

next it gets the length of the string.. creates a new string.. fills it in with the previous characters and checks the remainder.. if has an if statement with the % that's a command that gives you what's left over after dividing (ex: 6 divided by 3 is 2 right.. remainder would be 0.. 7 divided by 2 would have 1 left over.. hence what its checking) every time theres no remained on a divide by 3 it puts in a comma..

in c that method would fail.. because you would eventually go past the string length (by not accounting for the length when adding the comma).. but not sure if that would fail in java..

anyway, simple answer is it's changing 1000 into 1,000 :)
 
its not that bad.. first part just makes sure the value passed in itn't null.. if it is returns N/A..

next it gets the length of the string.. creates a new string.. fills it in with the previous characters and checks the remainder.. if has an if statement with the % that's a command that gives you what's left over after dividing (ex: 6 divided by 3 is 2 right.. remainder would be 0.. 7 divided by 2 would have 1 left over.. hence what its checking) every time theres no remained on a divide by 3 it puts in a comma..

in c that method would fail.. because you would eventually go past the string length (by not accounting for the length when adding the comma).. but not sure if that would fail in java..

anyway, simple answer is it's changing 1000 into 1,000 :)
So essentially it just ads a comma for every 3 digits... eg 123456789 = 123,456,789? I had a feeling that that was what it did, but I wasn't 100% sure.
Also in the first part, the string == null, isn't that technically wrong because doesn't doing that cause an exception? Like instead you'd have to say, If num == ""?
 
Like VinceT3 said the program should add a comma after every three digits or characters.

I happened to have the JCreator IDE installed (When I used to program in JAVA, C++ is so much better :D) and I get ArrayIndexOutOfBounds (You are going over the size of the array) for (sb[c++] = num.charAt(i);)

The string == null causes no problem for the compiler. (No exception is thrown)
 
I have another quick question. Again its been a long time, but what sort of method would you write to compare the contents of 2 large lists and see if any element in the 2nd list is an element of the first?

I'm really not that retarded with programming. Its just I haven't had to do programming for over a year, and that was in C# and ASP.Net, so I'm quite rusty.
 
You can sort both arrays and then looks to see if the same contents are there or the more efficient way would be to create a hash table for the first array and then hash each item in the second array and see if there are matches.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Total: 0, Members: 0, Guests: 0)

Who Read This Thread (Total Members: 1)

Latest posts