Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: problems running recursion, any help would be grealty appreciated!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems running recursion. [SOLVED]

    The program is supposed to check the summary total of a text document:

    Sample
    SUMMARY 01/29/2011 $-74.30 4
    BUY 01/20/2011 $-58.12
    SUMMARY 01/24/2011 $-104.69 2
    BUY 01/21/2011 $-92.51
    BUY 01/23/2011 $-12.18
    BUY 01/25/2011 $-0.37
    SELL 01/27/2011 $88.88

    the number after the amount is the number of sub entries which make up that summary. The problem I am having is when I try to run a recursion, after the second thread completes instead of returning to the first thread and finishing it, it just returns the current count and terminates the program.

    any help would be greatly appreciated!

    here's the segment I am having trouble with,

    he full code can be found at http://www.pastebay.net/307448

    here's the segment I am having trouble with,

     //method checks the accuracy of the summaries
        public static double subCheck(int sumNum, double subTotal, Scanner in) throws IOException{
     
                String date;
                String amountString =null;
                int x=sumNum;
                double total=subTotal;
                double amount=0;
                double count=0;
                double tTotal=0;
     
                while (x>0){
                        // calls lineCheck to determine the first token of the line
                amountString = lineCheck(in);
                //if summary is returned as the first token method autoNum is
                //called to get the number of sub entries that make up its total
                if(amountString=="SUMMARY"){
                        date = lineCheck(in);
                        total = amountNum(in);
                        tTotal=total;
                        //number of sub entries is stored as x
                        String X=subNum(in);
                        //string x is changed to an int
                        x=Integer.parseInt(X);
                        //amountString is converted to null
                        amountString =null;
                        if(x>0)
                        //method calls its self to check summary total
                        return subCheck(x,total,in);   
                }
                //checks the value of buy and sell, then adds the value into count
                if(amountString!="SUMMARY"){
                date=amountString;     
                amount=amountNum(in);
                count=amount+count;
                x--;
                //temporary, prints out count so I can check that the program is making the proper calculations
                System.out.println(count);           
                }
     
                }
     
                        return count;
    }
    Last edited by local127001; February 9th, 2012 at 06:58 PM. Reason: [SOLVED]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: problems running recursion, any help would be grealty appreciated!

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems running recursion, any help would be grealty appreciated!

    Sorry about that, I have updated my post.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: problems running recursion, any help would be grealty appreciated!

    One thing that would be problem is using == or != to compare Strings. You should use the equals() method.

    Your two if tests could be redundant.
    If the first one is true, will the second one always be false. And visa versa.
    Remove the second if and make it an else for the first if.

Similar Threads

  1. [SOLVED] n00b having problems running code
    By cha0s619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 10th, 2012, 03:31 PM
  2. Textpad Program Running Problems
    By Paddy in forum Java IDEs
    Replies: 4
    Last Post: January 27th, 2010, 08:14 PM
  3. any help is much appreciated
    By Schmitz14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2009, 07:51 PM
  4. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM
  5. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM

Tags for this Thread