can someone explain how i could go about applying this recursive method to a string of integers from a text file.

so far i have read the string into an int array in my main using a tokenizer and im unsure of how i should manipulate the array now.

also does the int list[] parameter in my method need to be initialized even though its being passed to the actual array parameter in the main?

my objective here is to recursively add the array of integers.
any good advice would be appreciated.


static int sumarray(int list[],int size)
{
int currentTotal;
int total;
if ( size == 0 )
total = 0;
else
currentTotal = sumarray( list, size - 1 );
total = currentTotal + list;
return total;
}

The folllowing is some sudo code for addition through recursion:

int sum_recursion(array, size) 
{
   if (size equals to 1) then ( return 0)
   else ( array[size -1] + sum_recursion(array, --size) );
}

You also have to type cast the the string numbers to integers while adding them, (if you already haven't done so). You should look in you text book if you don't know how to type cast. Hope this qualifies as good advice and good luck.

I hardly understand what you're doing or why, but here's the recursive solution.. Recursion is unnecessary and stack-dangerous, by the way. (StackOverflowErrors are ugly). Using a StringTokenizer is also unnecessary. I've included two examples, one is Your way, and the other is My way.

Your Way (Recursive and using a StringTokenizer)

public static void main (String[] args) {
	String IntegerString = "1 2 3        4  5     6 7 8   9 10";
	
	StringTokenizer Tokenizer = new StringTokenizer (IntegerString);
	
	int[] ints = new int[Tokenizer.countTokens()];
	
	for (int i = 0; i < ints.length; i++)
		try { ints[i] = Integer.parseInt (Tokenizer.nextToken()); }
		catch (Exception ex) { ints[i] = 0; /* PARSE ERROR */ }
		
	System.out.println ( "The sum of the integers is: " + Summarize (ints, ints.length) );
}
	
public static int Summarize (int[] ints, int curpos) {
	return (--curpos > 0 ? Summarize (ints, curpos) : 0) + ints[curpos];
}

// OR

public static int Summarize2 (int[] ints, int curpos) {
	if (--curpos < 0) return 0;
	return ints[curpos] + Summarize (ints, curpos);
}

My Way (Quicker, Fewer imports, No recursion)

public static void main (String[] args) {
	String IntegerString = "1 2 3        4  5     6 7 8   9 10";
	
	String[] Tokens = IntegerString.split ("\\s+");
	
	int[] ints = new int[Tokens.length];

	for (int i = 0; i < ints.length; i++)
		try { ints[i] = Integer.parseInt (Tokens[i]); }
		catch (Exception ex) { ints[i] = 0; /* PARSE ERROR */ }
		
	System.out.println ( "The sum of the integers is: " + Summarize (ints) );
}

public static int Summarize (int[] ints) {
	int Total = 0;
	for (int i : ints) Total += i;
	return Total;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.