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

Thread: Reading input into an ArrayList

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

    Default Reading input into an ArrayList

    Hey guys I need help reading input into an ArrayList which I will use to construct my polynomials
    any ideas on how to do this ??
    My code is below
     */
    package poly;
     
    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     *
     * 
     */
    public class Poly {
     
        private ArrayList<Integer> coefficients;
     
        public Poly()
        {
     
        }
        public Poly(ArrayList<Integer> coefficient) {
            this.coefficients = coefficient;
        }
     
        public void show() {
            for (int i = coefficients.size() - 1; i >= 0; i--) {
                if(coefficients.get(i)>=0 && i!=coefficients.size()-1)
                { 
                    System.out.print("+");
                }
                System.out.print(coefficients.get(i));
                if (i != 0) {
                    System.out.print("x");
                    System.out.print("^" + i);
                    System.out.print(" ");
                }
            }
            System.out.println("");// prints new line 
        }
     
        public Poly add(Poly otherPoly) {
     
     
            //We are going to add the coefficients of the current object as well as the 
            //object that is being passed on to the method
            ArrayList<Integer> sum = new ArrayList<Integer>();
            //We are checking if the degree of coefficients of the current object and the
            //object being passed into the method are the same
            if (this.coefficients.size() == otherPoly.coefficients.size()) {
     
                //Iterating over each element in the object.
                for (int i = 0; i < coefficients.size(); i++) {
     
                    //Adding the coefficients of the currrent object and the correspondeing
                    //coefficients the object that is being passed.
                    sum.add(this.coefficients.get(i) + otherPoly.coefficients.get(i));
     
                }
     
     
            }
            //Creating a new polynomial to pass the arraylist that has just  been created.
            Poly sumPoly = new Poly(sum);
            //Returning the polynomial.
            return (sumPoly);
        }
     
        public Poly subtract(Poly otherPoly) {
     
     
            //We are going to add the coefficients of the current object as well as the 
            //object that is being passed on to the method
            ArrayList<Integer> takeAway = new ArrayList<Integer>();
            //We are checking if the degree of coefficients of the current object and the
            //object being passed into the method are the same
            if (this.coefficients.size() == otherPoly.coefficients.size()) {
     
                //Iterating over each element in the object.
                for (int i = 0; i < coefficients.size(); i++) {
     
                    //Adding the coefficients of the currrent object and the correspondeing
                    //coefficients the object that is being passed.
                    takeAway.add(this.coefficients.get(i) - otherPoly.coefficients.get(i));
     
                }
     
            }
     
            //Creating a new polynomial to pass the arraylist that has just  been created.
            Poly takeAwayPoly = new Poly(takeAway);
            //Returning the polynomial.
            return (takeAwayPoly);
        }
     
     
    public void read()  
    {
     Scanner in= new Scanner(System.in);
     System.out.println("Please Enter the degree of the polynomial");
     int degree=in.nextInt();
     for(int i=0; i<=degree; i++)
     {
         System.out.println("Please enter the enter coefficient for the"+ i+"exponent");
     }
     int[] coefficientArray=new int[degree];
     
    }
     
    //MAIN CLASS BELOW 
     
     public static void main(String[] args) {
           // int[] coefficient = {4, 3, 2};
            ArrayList<Integer> coefficients = new ArrayList<Integer>();
            coefficients.add(4);
            coefficients.add(2);
            coefficients.add(1);
            coefficients.add(4);
     
            Poly one = new Poly(coefficients);
     
            ArrayList<Integer> coefficients_2 = new ArrayList<Integer>();
            coefficients_2.add(5);
            coefficients_2.add(3);
            coefficients_2.add(1);
            coefficients_2.add(5);
     
            Poly two = new Poly(coefficients_2);
     
            one.show();
            two.show();
     
            //Need to confirm if not printing the error mesage is okay.
            Poly three = one.subtract(two);
            three.show();
     
            Poly four = one.add(two);
            four.show();
     
     
     
     
        }
    }
    Last edited by thesoulpatchofBruce; October 11th, 2012 at 02:46 PM. Reason: putting tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading input into an ArrayList

    Please edit your post and wrap the code with code tags, and be sure the formatting is 'decent' at least. See the forum announcements page if you have questions on using tags.

Similar Threads

  1. Reading input from a CMD operation?
    By Yulfy in forum Java Theory & Questions
    Replies: 1
    Last Post: February 3rd, 2012, 10:41 AM
  2. Reading a text file into an arraylist of fueldispensers.
    By vbhatti in forum Object Oriented Programming
    Replies: 3
    Last Post: November 20th, 2011, 12:17 PM
  3. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  4. Reading String Input?
    By Morevan in forum Java Theory & Questions
    Replies: 1
    Last Post: January 18th, 2010, 11:16 AM
  5. reading JMF input streams?
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 11th, 2010, 11:22 PM