| | |
help with recursive method
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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.
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[size - 1];
return total;
}
•
•
Join Date: Oct 2006
Posts: 30
Reputation:
Solved Threads: 3
The folllowing is some sudo code for addition through recursion:
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.
Java Syntax (Toggle Plain Text)
int sum_recursion(array, size) { if (size equals to 1) then ( return 0) else ( array[size -1] + sum_recursion(array, --size) ); }
Last edited by Rayhan Muktader; Oct 5th, 2006 at 4:13 pm.
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)
My Way (Quicker, Fewer imports, No recursion)
Your Way (Recursive and using a StringTokenizer)
Java Syntax (Toggle Plain Text)
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)
Java Syntax (Toggle Plain Text)
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; }
Last edited by Cudmore; Oct 6th, 2006 at 6:55 pm. Reason: Improvement
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
![]() |
Similar Threads
- permutation of a string in c++ (C++)
- C++ Performance Tips (C++)
- Theoretical question: poly root finding (secant method) (Computer Science)
- recursive linked list (C++)
- binary search (C)
- verifying a Binary Search Tree (Java)
- source code to search windows system (Java)
Other Threads in the Java Forum
- Previous Thread: Escaping
- Next Thread: algorithm problem with recursion
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





