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 7 of 7

Thread: Need help using Recursion in an assignment

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Need help using Recursion in an assignment

    Here is the assignment:
    Overview
    Write a program that reads a list of many-digit integers from a file, adds them together, and
    writes the result to an output file.
    You may not make any assumptions about the maximum number of digits that a number will
    have. A reasonable linked-list implementation of the solution should run successfully with
    the instructorís test data on the instructorís machine.
    The format of the numbers will be the same as for valid real numbers in Java. You may
    assume that scientific notation will not be used for the input (and should not be used for the
    output).

    Input
    Your input will be in a text file containing several numbers, one to a line. An example of
    what the input might look like is:
    11111111111111111111111111111111111111111111111111 0
    -11111111111111111111111111111111111111111111111111 1
    22222222222222222222222222222222222222222222222222 22
    33333333333333333333333333333333333333333333333333 333
    -4


    Output
    Your output will be to another file, and will echo the input and give the result. Output for
    the preceding input example might look like:
    The sum of
    11111111111111111111111111111111111111111111111111 0
    + -11111111111111111111111111111111111111111111111111 1
    + 22222222222222222222222222222222222222222222222222 22
    + 33333333333333333333333333333333333333333333333333 333
    + -4
    is
    35777777777777777777777777777777777777777777777777 772
    Processing
    The only processing requirement I am placing on this programming project is that it use a
    linked list to store the numbers (and not BigInteger objects).
    There is one additional major consideration in building your long number, though: the sign.
    Adding negative numbers means doing subtraction and allowing for negative results.
    One other suggestion I would make is that your program deal with each input number and
    calculate a subtotal before processing the next number. Since you donít know ahead of
    time how many numbers you will need to process, storing them all is probably more difficult
    than processing each as it is read from the input file.

    import java.io.File;
    import java.util.Scanner; 
    import java.util.*;
    import java.util.LinkedList;
     
    public class Homework3 {
        public static void main(String[] args) throws Exception{
     
     
           File file = new File("linklist.txt");
     
           Scanner input = new Scanner(file); 
     
           LinkedList numbers = new LinkedList();
     
           System.out.println("The sum of ");
     
           while (input.hasNext()) {
               String num = input.nextLine();
               numbers.add(num);
               System.out.println(num); //Display ea number
           }
     
            System.out.println("is");
            System.out.println("");
     
     
               //Right here is where I need the code help!!!!!!!!!!!!!!!!!!!!!
     
           //Add the list items together one number at a time. Find out how long the string of each one is.
            input.close();
        }
    }

    I need help in finding out how to add these numbers in a string together


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,453
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: Need help using Recursion in an assignment

    How would you do it "by hand" without a computer?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help using Recursion in an assignment

    One number at a time starting from the right and if it is greater than 9 add 1 to the next numbers being added. My question is how would i put that into code. I am just lost. I know it has to do with char.At and some crazy loops but I can't figure it out.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,453
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: Need help using Recursion in an assignment

    If you're supposed to use recursion, then I doubt you'd also want to use "crazy loops". Check out the API for String and maybe Integer for useful functions.

    String (Java Platform SE 6)
    Integer (Java Platform SE 6)

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help using Recursion in an assignment

    import java.io.File;
    import java.util.Scanner; 
    import java.util.*;
    import java.util.LinkedList;
     
    public class Homework3 {
        public static void main(String[] args) throws Exception{
     
     
           File file = new File("linklist.txt");
     
           Scanner input = new Scanner(file); 
     
           LinkedList numbers = new LinkedList();
     
           System.out.println("The sum of ");
     
           while (input.hasNext()) {
               String num = input.nextLine();
               numbers.add(num);
               System.out.println(num); //Display ea number
           }
     
            System.out.println("is");
            System.out.println("");
     
     
               //Right here is where I need the code help!!!!!!!!!!!!!!!!!!!!!
     
           //Add the list items together one number at a time. Find out how long the string of each one is.
            input.close();
        }
    }


    What I personally don't get is why you would be using a LL in the first place to store each string and then add all the number together at one and not just process one at a time...
    One word of advice, since the numbers you are dealing with are so huge I would suggest that you use a long representation for them instead of an int one.

    so here's how I'd I'd approach the problem:

    import java.io.File;
    import java.util.Scanner; 
    import java.util.*;
    import java.util.LinkedList;
     
    public class Homework3 {
        public static void main(String[] args) throws Exception{
     
     
           File file = new File("linklist.txt");
     
           Scanner input = new Scanner(file); 
     
           long number=0;
     
           System.out.println("The sum of ");
     
           while (input.hasNext()) {
               String num = input.nextLine().trim(); //trim(): delete eventual whitespaces
               number+=(new Long(num)).longValue();  //adding your num to number by converting it to a long (to do so you have to create a new Long() by passing num to it and then get its long value);
               System.out.println(num); //Display ea number
           }
     
            System.out.println("is");
            System.out.println("");
     
     
              System.out.println(number);
     
            input.close();
        }
    }

    I think you'll be able to manage creating the output file by yourself

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,453
    My Mood
    Hungover
    Thanks
    144
    Thanked 638 Times in 546 Posts

    Default Re: Need help using Recursion in an assignment

    Quote Originally Posted by lex25288 View Post
    so here's how I'd I'd approach the problem:
    I didn't look too closely at your solution, but it doesn't seem to use recursion at all, which was the point of the assignment.

    This is one of the many reasons that spoonfeeding is not helping.

    Either way, this post is over a month old, so I assume that the assignment is over anyway.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Need help using Recursion in an assignment

    I had done the same program in 'C' language.

    take 3 array list
    you can simply read two numbers in a arraylist or link list of char.

    find the size of two array list

    the list has length greater take that list as first number and other as second.

    int carry = 0;
    int sum = 0 ;

    for(int k=0, i= num2List.length()-1 ; i>=0 ; i--,k++)
    {
    sum =Integer.parseInt( num2List[i]) +Integer.parseInt (num1List[i]) + carry;

    if(sum>9){
    carry = sum /10;
    sum = sum %10 ;
    }
    else
    carry =0;

    result[k] = sum;

    }

    for(int j = k-1; j>=0 ; j-- )
    {
    sum = Integer.parseInt(num1List[j]) + carry;

    if(sum>9){
    carry = sum /10;
    sum = sum %10 ;
    }
    else
    carry =0;

    result[j] = sum;
    }

    Now your result is stored in reverse order in result array list you can write the result into the file now.

    Please don't copy paste code as it is , do necessary modification (like : typecasting , arraylist declaration and others)
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  2. recursion problem, please help
    By nil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2010, 12:59 PM
  3. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 10:50 PM