Hi all,
I have a method set to return an array of type integer but I would like to know how I would access the independent values in that returned array. My program does this:
User enters numbers seperated by spaces and the program computes such things like the average, sum, product and minimum and maximum of those numbers.
I used the "StringTokenizer" class to allow the user to enter the numbers all at once without having to press enter in between. The method mentioned looks like this:
// Method to break up the string and place it inside an array
public static int[] breakUp(String myString)
{
StringTokenizer sT = new StringTokenizer(myString);
// Get the tokens and store them in an array, performing the conversion along the way
int length = sT.countTokens();
int [] myArray = new int[length];
for( int i = 0; i < myArray.length; i++ )
{
int currentValue = Integer.parseInt( sT.nextToken().trim() );
myArray[i] = currentValue;
}
return myArray;
// Used to check to see if the method places the tokens into the array correctly
// printArray(myArray);
}
The point of this method is to save space and code because I don't want to include the declaration of the "StringTokenizer" in every method but still want to access the array easily.
Additional Information:
i) User data entered as a String
ii) String broken up into tokens
iii) Convert tokens to integer
iv) Integer's placed in an array for easy manipulation - sometimes...
Cheers :D