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

Thread: Vectors

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Vectors

    I have to create a program which reads and processes an input file with account transactions ordered by transaction date
    and generates a report every time the date of the current transaction being processed is different than the last processed transaction

    This is what I have so far
    //importing packages needed to create class
    import java.util.*;
    import java.io.*;
    //creating class account which will include constructors, accessors and mutator methods
    public class Account
    {
        //initializing variables
        private static int accountId;
        private static double balance;
     
        //default constructor
        public Account()
        {
            accountId = 0;
            balance = 0.0;
        }
        //constructor which will take in parameters 
        public Account(int accountId, double balance)
        {
            this.accountId = accountId;
            this.balance = balance;
        }
     
        //mutator method which will change variable accountId
        public void setId(int id)
        {
            accountId = id;
        }
     
        //accessor method which will return the value stored in Accountid
        public int getAccountId()
        {
            return accountId;
        }
     
        //mutator method which will change variable balance
        public void setBalance(double balance)
        {
            this.balance = balance;
        }
     
        //accesor method which will return the value stored in balance
        public double getBalance()
        {
            return balance;
        }
    }

    my main class
    //Mario Gutierrez, CS_2401, MWF 8:30 a.m.
    //importing packages needed to create class
    import java.util.*;
    import java.io.*;
    /*this program will read and process an input file and generates a report every time
    **the date of the current transaction changes
    */
    public class BankAccount
    {
            private static char action;
            private static int acctId;
            private static long date;
            private static double bal;
            private static int count;
     
     
        //creating main method where report will be generated
        public static void main(String[] args)throws FileNotFoundException
        {
            //creating scanner which will read input file
            Scanner input = new Scanner(new FileReader("input.txt"));
            input.useDelimiter(",\\s*");
     
            //initializing variables
            Vector<String> bank = new Vector<String>();
     
          while(input.hasNext())
          {
              date = input.nextLong();
              acctId = input.nextInt();
              action = input.next().charAt(0);
              bal = input.nextDouble();
              System.out.print(date);
              System.out.print(" ");
              System.out.print(acctId);
              System.out.print(" ");
              System.out.print(action);
              System.out.print(" ");
              System.out.print(bal);
              System.out.println();
     
          }
          System.out.print(bank);
          input.close();
        }
    }
    What i dont know how to do is add those elements from the input file and place them in my vector?
    can anyone help me to finish this program thank you


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Vectors

    Can anyone at least help me and show me how to create a vector and add elements into it from an input file?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Vectors

    Please don't post the same question in multiple places.

    I'd recommend that you instead use an ArrayList. They both accomplish basically the same thing except vectors are thread-safe. If you want to use an ArrayList in a multi-threaded program, you can create a SynchronizedList from the ArrayList.

    To create a new ArrayList:

    // Vectors are pretty much the same, just replace ArrayList with Vector
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5); // add the value 5 to the end of the ArrayList

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Vectors

    Its part of my assignment to use vectors so I have to use vectors or else I would use an arraylist

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Vectors

    All you need to change from the above code then is to replace every "ArrayList" with "Vector" and it should work.

Similar Threads

  1. Replies: 3
    Last Post: November 15th, 2008, 06:17 AM