Call the method:
int [] array = breakUp("some string");
array[0];
array[1];
array[2];
....
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
return myArray;
// Used to check to see if the method places the tokens into the array correctly
// printArray(myArray);
This is not just a check; the return value is the only result of the method. myArray is a local variable - when you return from the method that created it, the variable goes out of scope and the results of your calculation are lost, except for the reference to them that the method returns.
There is no way to access myArray outside of your breakUp() method, unless you save a copy of it in the manner javaAddict suggests.
To be very clear: the String "myString" will not be changed after this method is run, and none of the variables declared in the body of the method will exist when it's finished. This is the difference between local variables and class fields.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
You might want to look at that code again. Line 15 seems a little dodgy to me. :)
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Does it? It looks to me like this sequence:
1, 2, 3, 4, 5
should go fine
and
5, 4, 3, 2, 1
should go fine
but
1, 2, 5, 3, 4
should end up with minimum == 4
(on the last loop, maximum == 5. 4= to maximum - it's always == maximum, to be precise)
In reverse sequence, the two take these values:
5,5 5,4 5,3 5,2 5,1 (because i < maximum in each case - lucky you, the last value of i is the true minimum)
Methinks your testing may not have been very extensive. :)
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Sure, validation of input is always nice. But it'll work even betterer if the comparison in line 15 is to minimum, rather than to maximum.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187