Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,309 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 863 people online right now. Registration is fast and FREE... Join Now!




java array

 
Reply to this topicStart new topic

java array, array

rhj
14 Nov, 2007 - 11:37 AM
Post #1

New D.I.C Head
*

Joined: 6 Nov, 2007
Posts: 10


My Contributions
CODE
import java.util.Scanner;
public class Advisor
{
    public int i;
    public String a;
    private String Aname;
    Advisor Advisor = new Advisor(Aname);
    
    Advisor[] Aarray = new Advisor[9];

        public static void main( String args[] )
        {
            Advisor Advisor = new Advisor("");
            Advisor.display();
        }
        public Advisor( String Name )
        {
            Aname = Name;
        }
        public void setAdvisor( String Aname )
        {
            this.Aname = Aname;
        }
        public String getAdvisor()
        {
            return Aname;
        }
        public void display()
        {
            
            Scanner Scan = new Scanner( System.in );
            for(i=0;i<=Aarray.length;i++)
            {
                System.out.println("Advisor Name: ");
                a=Scan.next();        
                Aarray[i].setAdvisor( a );
            }
            
        }
}




i want to declare 10 advisors and saving their name in an array.but i dont know how to send their String name because when im using the Aarray[i].setAdvisor(a); it doesnt work.thanx

User is offlineProfile CardPM
+Quote Post


brawnyman713
RE: Java Array
14 Nov, 2007 - 01:39 PM
Post #2

D.I.C Head
Group Icon

Joined: 21 Oct, 2007
Posts: 139


Dream Kudos: 125
My Contributions
If you have an array of objects, you dont create a name for each one. Instead, the values are accessed like this:

CODE


//This is your object array(max length of ten)
Advisor[] advisors = new Advisors[10];

//Access the adviseMe method(I made it up) for the second object
advisors[1].adviseMe();



If I wasnt clear on anything, let me know
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Java Array
14 Nov, 2007 - 01:59 PM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,446



Thanked: 55 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
I see a couple of problems here:
CODE

private String Aname;
    Advisor Advisor = new Advisor(Aname);

the variable Aname has no value, so odds are there may be a problem here.
on top of that, you named the object the same as the class, if you want to do that, then use Advisor advisor the case matters.

I am a little uncertain how you know the names aren't being saved, you do not print them out anywhere. Also you can call Advisor(); the default constructor always exists, even if you don't declare it.

Assuming the values aren't being saved, try:
Aarray[i].setAdvisor( a.toString() );
this will guarantee you are passing a String, thus the error is somewhere else.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 07:16PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month