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: Array code not producing, thoughts?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Array code not producing, thoughts?

    OutPut is supposed to be: Size: 3
    first account number is 1008
    last account number is 1729

    Sub
    import java.util.ArrayList; 
    public class BankAccount2{
    	public BankAccount2(int anAccountNumber) {
    		accountNumber = anAccountNumber;
    		balance = 0;
    	}
    	public BankAccount2(int anAccountNumber, double initialBalance){
    		accountNumber = anAccountNumber;
    		balance = initialBalance;
    	}
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	public void deposit(double amount){
    		double newBalance = balance + amount;
    		balance = newBalance;
    	}
    	public void withdraw(double amount){
    		double newBalance = balance - amount;
    		balance = newBalance;
    	}
    	public double getBalance(){
    		return balance;
    	}
    	private int accountNumber;
    	private double balance;
    	}

    Main
    public class ArrayListTester {
    	public static void main(String[] args) {
    		ArrayList<BankAccount2> accounts = new ArrayList<BankAccount2>();
    		accounts.add(new BankAccount2(1001));
    		accounts.add(new BankAccount2(1015));
    		accounts.add(new BankAccount2(1729));
    		accounts.add(1,new BankAccount2(1008));
    		accounts.remove(0);
    		System.out.println("size" + accounts.size());
    		BankAccount2 first = accounts.get(0);
    		System.out.println("first account number is: " + first.getAccountNumber());
    		BankAccount2 last = accounts.get(accounts.size()-1);
    		System.out.println("last account number is: " + last.getAccountNumber());
    	}
    }
    This program is in my notebook, was testing professor's program but it isn't functioning..


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Array code not producing, thoughts?

    Quote Originally Posted by r19ecua View Post
    OutPut is supposed to be: Size: 3
    first account number is 1008
    last account number is 1729

    Sub
    import java.util.ArrayList; 
    public class BankAccount2{
    	public BankAccount2(int anAccountNumber) {
    		accountNumber = anAccountNumber;
    		balance = 0;
    	}
    	public BankAccount2(int anAccountNumber, double initialBalance){
    		accountNumber = anAccountNumber;
    		balance = initialBalance;
    	}
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	public void deposit(double amount){
    		double newBalance = balance + amount;
    		balance = newBalance;
    	}
    	public void withdraw(double amount){
    		double newBalance = balance - amount;
    		balance = newBalance;
    	}
    	public double getBalance(){
    		return balance;
    	}
    	private int accountNumber;
    	private double balance;
    	}

    Main
    public class ArrayListTester {
    	public static void main(String[] args) {
    		ArrayList<BankAccount2> accounts = new ArrayList<BankAccount2>();
    		accounts.add(new BankAccount2(1001));
    		accounts.add(new BankAccount2(1015));
    		accounts.add(new BankAccount2(1729));
    		accounts.add(1,new BankAccount2(1008));
    		accounts.remove(0);
    		System.out.println("size" + accounts.size());
    		BankAccount2 first = accounts.get(0);
    		System.out.println("first account number is: " + first.getAccountNumber());
    		BankAccount2 last = accounts.get(accounts.size()-1);
    		System.out.println("last account number is: " + last.getAccountNumber());
    	}
    }
    This program is in my notebook, was testing professor's program but it isn't functioning..

    You can add "=Java" to the first code tag to highlight the Java elements.

    Can you post the output of the program?

  3. #3
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Array code not producing, thoughts?

    Dude, I got the exact output you wanted when I ran this program.
    Size: 3
    First account number is: 1008
    Last account number is: 1729

    What is not working for you?

  4. #4
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Array code not producing, thoughts?

    Oh I think I know what it is...

    You've declared "import java.util.ArrayList" in the BankAccount2 class and not in the ArrayListTester class. I presume since you have BankAccount2 as a public class, its not in the same file as ArrayListTester which then would arise a problem since the ArrayListTester would have no clue what an ArrayList is....

    Does that fix it for you...? Code works fine for me. I've compiled it both as two seperate java files and one big file and they both produce the expected outcome

Similar Threads

  1. Duplicating array in C code
    By FearTheCron in forum Java Native Interface
    Replies: 1
    Last Post: April 6th, 2013, 09:33 PM
  2. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  3. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM
  4. Array Code Help
    By whattheeff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 04:44 PM
  5. Need help with array code
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 10:54 PM