Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

buffingtonr

macrumors newbie
Original poster
Nov 11, 2008
4
0
Nashville, TN
Hey Friends,

I spent Friday afternoon trying to solve a pretty simple problem in Objective-C. I played around with it for two hours before taking 10 minutes to write it in Java. I've got to ask the question, what the heck am I doing wrong? I don't have a lot of experience with C-arrays and memory management. Furthermore, I'm just not sure if an NSArray of objects is the solution to this problem.

I pose a question to the gurus of the language, as I am still relatively new to it (4 months in). The following code is Java. What is the best way (or what are some ways) to translate this code into Objective-C?

Code:
[COLOR="SeaGreen"]/**  [B]Java Code[/B]  **/[/COLOR]
private static String equalAdditions(String s) {
[INDENT]String[] terms = s.split(" "); [COLOR="SeaGreen"]    // Input String: "123 456"[/COLOR]
char[] a = terms[0].toCharArray(); [COLOR="SeaGreen"]// Char Array a: { '1', '2', '3' }[/COLOR]
char[] b = terms[1].toCharArray(); [COLOR="SeaGreen"]// Char Array b: { '4', '5', '6' }[/COLOR]
String diffString="";

for(int i=b.length-1; i>=0; i--) {
[INDENT]if(a[i]<b[i]) {
[INDENT]int x=( (int) a[i]+10 ) - ( (int) b[i] );
diffString = x + "" + diffString;
b[i-1]++;[/INDENT]
}
else {
[INDENT]diffString = "" + (a[i]-b[i]) + diffString;[/INDENT]
}[/INDENT]
}
return diffString;[/INDENT]
}

PS: if anyone is wondering what the purpose of this code is, it is the algorithm of "Equal Additions" a method of subtracting by hand. Obviously, it would be easier to code num1-num2 but that is not my purpose. Also, I never fully debugged this Java code to make sure it did what I wanted it to do, but it looks like it works with any integer where String1 >= String2 and String1's length is equal to String2's length.

Code:
[B][U]Algorithm:[/U][/B] (not important but provided for the curious)

Input: String representation of two numbers
Output: number1 - number2
[I][B]Assume[/B] both numbers are the same amount of digits
[B]Assume[/B] number1 is >= number2 (ie: no negative outputs)[/I]
1: Put two numbers into two separate arrays of single digits
2: Work backwards subtracting digit(n) of Number2 from digit(n) of Number1, altering array where necessary. 
[INDENT]If Number1.digit(n) < Number2.digit(n), add 10 to Number1.digit(n), add 1 to Number2.digit(n-1).[/INDENT]
3: Return the difference of Number1 and Number2.

Thanks for the help. Happy Travels!

< Buffalo >
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
In your mind, NSString should be your new java.lang.String.
Code:
NSString *myString = @"421";
NSString *myString2 = @"123";
NSMutableString *resultString = [[NSMutableString alloc] init];
unichar charOne,charTwo;
if([myString length] != [myString length]) {
  NSLog(@"Invalid string lengths");
}
for(int pos=[myString length]-1;pos >= 0; pos--) {
  charOne = [myString characterAtIndex:pos];
  charTwo = [myString2 characterAtIndex:pos];
  //Do your voodoo math, when you have something to append:
  [resultString appendString:[NSString stringWithFormat:@"%d",x-y]];
}

return result;

I haven't compiled this, so... it will take some work to actually get it going. NSMutableString might seem uncomfortable coming from java, but note that java.lang.String in java is also unmutable, and often this gets people in trouble because they're generating a ton of "new" strings and assigning them to the same place. Think of NSMutableString as a sort of StringBuilder.

Good luck.

-Lee

P.S. I read this thread at first hoping we were going to be moving data between Obj-C and Java via JNI. That would be much more fun.
 

buffingtonr

macrumors newbie
Original poster
Nov 11, 2008
4
0
Nashville, TN
Lee,

Thanks for the help!

I see we are not using any arrays in this method. That's fine. My question is, how does Objective-C treat a uniChar when you want to do math on it as an integer. How would one convert the unichar to an integer? Thanks again!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
unichar is just an unsigned short.

Tip: command-click in Xcode any variable/class/etc and you will go to its definition.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.