Results 1 to 9 of 9
  1. #1
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Where am I going wrong with arrays

    Can anyone tell me what I am doing wrong with arrays? Account is an object on the account class and I want to create an array of 10 accounts.

    Java Code:
    
     class accountAR {
    
    public static void main (String [] args)  {
    
      	Account[] acc = new Account[10];
    	acc[0]= new Account ();
    
    
    
    for (int x = 0; x<10;x++)
     {
    acc[x] = new Account ();
    acc[x].setId (x);
    acc[x].setBalance(50);
    }
    
    
    
    System.out.println (" the balance of account 1 is " + acc[2].getbalance() );
    
    
    }
        }
    Last edited by r1b; 01-08-2012 at 04:18 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Where am I going wrong with arrays

    Please please format your code so that it is readable because right now it isn't. If you want volunteers here to take the time and effort out of their day to help you, it really isn't asking too much for you to put in the small effort to make it possible for them to do so.

    Also, correct formatting will often reveal errors that you've not seen before. For instance why do you have a naked code block at the beginning of your class?
    Last edited by Fubarable; 01-08-2012 at 03:45 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Where am I going wrong with arrays

    Also, when you post your corrected your code, you'll want to post any and all error messages you're receiving and indicate which line, by comment, is causing the error.

  4. #4
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Where am I going wrong with arrays

    Sorry ,I will remember comments next time Fubarable. Basically I am trying to create an AtmMachine that creates 10 accounts with a balance of 50 and increments
    the id variable during creation.

    Would it help if I posted my account class?

  5. #5
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Where am I going wrong with arrays

    My account class

    Java Code:
    import java.util.*;
    
    public class Account {
    
    
    private static int id;
    private String name;
    private double balance;
    private int pinNumber;
    private boolean isActive;
    private Date dateCreated = new Date();
    private double annualInterestRate;
    
    
    
    public Account (int a, String b ) {
    
    int id = a;
    String name = b ;
    double balance;
    int pinNumber = 1386;
    boolean isActive = false;
    Date dateCreated = new Date();
    double annualInterestRate = 4.4;
    
    }
    
    // no agr constructor
    public Account (){};
    
    
    // set the pinNumber
    protected void  setpinNumber (int input) {
    pinNumber = input; }
    
    protected void setId (int inputID) {
    	id = inputID;}
    
    protected int getpinNumber () {
    return pinNumber;
    }
    
    
    protected boolean testpinNumber ( int entry) {
    
    if ( pinNumber == entry  ) {
    isActive = true ;
    }
    else
    {
    System.out.println (" Please re-enter your PIN number");
    isActive = false;
    
    }
    return isActive;
    
    }
    
    
    protected int getid () {
    return id;}
    
    
    protected String getname () {
    return name;}
    
    
    protected void setBalance ( double c) {
    	balance = c;}
    
    protected double getbalance () {
    return balance;}
    
    protected boolean isActive () {
    return isActive;}
    
    protected void deposit( double amount )
    	{
    		balance += amount;
    	}
    
            public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			balance -= amount;
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
                            return 0.0;
    	}
    
    	protected void setannualInterestRate( double rate) {
    
     		annualInterestRate = rate ;
    		}
    
    // int for the 12 months of the year
    
    public double m  = 12;
    
     // method to get the monthly interest rate
    
    	protected  double getmonthlyInterestRate() {
    	 return annualInterestRate / m ;
    
    	    	}
    
    protected Date getDate () {
    	return dateCreated ;}
    
    
    
    }
    
    class TestAccountClass {
    
    
    	public static void main (String [] args)
    
    	{
    
    
    // create the object
    
    Account test = new Account (1122 ,"Robert Smith");
    
    //set the annual interest rate
    
             test.setannualInterestRate (4.55);
    
    // set balance of test
            test.setBalance (20000.00);
    
            // Deposit money
    		test.deposit (3000.00);
    
                    // Print current balance
    		System.out.println ("Current balance " +
    			test.getbalance());
    
                    // Withdraw money
    		test.withdraw(2500.00);
    
                    // Print remaining balance
    		System.out.println ("Remaining balance " +
    			test.getbalance());
    
    		System.out.println ("your current monthly interest rate is " +
    			test.getmonthlyInterestRate () );
    
    
    			// Print date of account creation
    		System.out.println ("Your account was created on " +
    			test.getDate());
    
    }}

    This is fine. I am just confused with arrays.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Where am I going wrong with arrays

    Quote Originally Posted by r1b View Post
    Sorry ,I will remember comments next time Fubarable. Basically I am trying to create an AtmMachine that creates 10 accounts with a balance of 50 and increments
    the id variable during creation.

    Would it help if I posted my account class?
    Post your account class, sure, but please clean up and re-format your code -- this is your responsibility. Again, it is unreadable with your use of random indentations and with your bunching of curly braces on the same line. Again if you fix it, you'll see that you've got a naked code block in the beginning of your accountAR class.

    If you're not sure how to format it, look at examples here and in tutorials. It will show you that you should indent a standard amount (I use 3-4 spaces), where to indent and not indent. If your code isn't clean, it isn't readable. And if it's not readable, many here will hold off helping you until you've fixed it. Again, we're volunteers putting in free time to help you. Again, it's not asking too much to request you do this little chore -- is it?

    And you're not confused with arrays. You're confused with scoping of variables. I'll tell you more if/when you fix your code.

  7. #7
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Where am I going wrong with arrays

    Sorted Fubarable. Thanks

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Where am I going wrong with arrays

    Hm, you've changed your code with the format. Previously you had this (formatting corrected):
    Java Code:
    class accountAR {
    
       Account[] acc = new Account[10];
    
       {
          acc[0] = new Account();
    
          for (int x = 0; x < 10; x++) {
             acc[x] = new Account();
             acc[x].setId(x);
             acc[x].setBalance(50);
          }
       }
    
       public accountAR() {
          System.out.println(" the balance of account 1 is" + acc[0].getbalance());
       }
    }
    and this:
    Java Code:
    class accountARtest {
       public static void main(String[] args) {
          System.out.println(" the balance of account 1 is " + acc[2].getbalance());
       }
    }
    Now you've got this (formatting corrected):
    Java Code:
    class accountAR2 {
    
       public static void main(String[] args) {
    
          Account[] acc = new Account[10];
          acc[0] = new Account();
    
          for (int x = 0; x < 10; x++) {
             acc[x] = new Account();
             acc[x].setId(x);
             acc[x].setBalance(50);
          }
    
          System.out.println(" the balance of account 1 is " + acc[2].getbalance());
    
       }
    }
    Again, your formatting is off -- please note the differences between my code and yours. You will want to study a formatting specification soon to learn how to do it correctly.

    But more importantly -- which code is the one you're having problems with, and again what errors are you seeing?

    Edit: you can find the Java Code Conventions here: http://java.sun.com/docs/codeconv/CodeConventions.pdf
    Last edited by Fubarable; 01-08-2012 at 07:35 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Where am I going wrong with arrays

    To clarify,

    Line 5 in the original accountAR class (the first bit of code that is posted in my reply just above this one) is the beginning of what's called an "initializer block", a block of code that is called when a class is created and before any constructor is called. I'm pretty sure that you didn't want to do this, but I'm not sure where you wanted that code.

    On line 3 of your accountARtest class you try to access the acc variable, but there is no acc variable in this class, and so your compiler will complain. Yes the variable exists in the accountAR class, but that's not how you access other class's variables -- and in fact you shouldn't try to directly manipulate another class's variables regardless.

    Your second accountAR class, the one you posted in your edit to the original post and one I've renamed accountAR2 is completely different in that all code is in a main method and you have no instance variables or instance methods. If you're having problems with this code, you don't say since you have not posted any error messages or exception messages.

    So your question is still unanswerable. For more help and decent suggestions on how to make your questions more easily answered, please check out my signature link below on how to ask smart questions.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  3. Replies: 4
    Last Post: 03-15-2011, 03:58 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •