aspose file tools*
The moose likes Beginning Java and the fly likes Need help adding people to array through a menu!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help adding people to array through a menu!!" Watch "Need help adding people to array through a menu!!" New topic
Author

Need help adding people to array through a menu!!

Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Hi people,

Just reporting another problem�

I have 2 classes. One is person class and one is lottery class.

Now I want the user to add a person through the menu, so in person class I have declared the variables and set get and set methods like this�



Now in the lottery class I am trying to add the person, but i have compilation errors.


Can you see anything wrong with the code. Sorry I am a beginner so I am learning by making mistakes.

Thanks for your help.
[edit]Add code tags. CR[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
First things first:



Hint: what are you setting, and are you return something to the void?

Second,



Is the employee filled with references to objects, or are they still null? When you create an array


All the elements will be set to null. So you don't have 100 SomeClass objects now.

Work on it some more and don't forget to add good comments to your code, and ask questions that may arise if you want to

Good luck.


Java hobbyist.
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Originally posted by Ronald Schild:
First things first:



Hint: what are you setting, and are you return something to the void?

Second,



Is the employee filled with references to objects, or are they still null? When you create an array


All the elements will be set to null. So you don't have 100 SomeClass objects now.

Work on it some more and don't forget to add good comments to your code, and ask questions that may arise if you want to

Good luck.


ok first of all, let me change the set methods to the following..

public void setName(String name)
{
return name;
}

public void setNumber(int number)
{
return number;
}


secondly, i have a declared a array of employee's with the following line...

Person employee[] = new Person [50];

I still havent got the slightest clue in what i need to do next..

As you can tell, i am a beginner
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3673
    
  15
Originally posted by Waria Ahmed:


ok first of all, let me change the set methods to the following..

public void setName(String name)
{
return name;
}

public void setNumber(int number)
{
return number;
}


void in a method signature means that the method cannot return anything. Instead you should be setting the instance variablesto the value of the parameters passed to the method.
e.g.You refer to the instance variable using 'this' (meaning the current instance) to differentiate it from the parameter which has the same name.
[ July 30, 2008: Message edited by: Joanne Neal ]

Joanne
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Ok i have changed the set methods as you said and changed the add person code to the following..




Now i get a inputMisMatch exception??? Am i doing anything else wrong?

Thanks

[edit]Add code tags. CR[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3673
    
  15
An InputMismatchException is thrown by the Scanner class when it fails to read the type of input it is expecting. The stack trace will tell you what line it is on, but it is probably this linean you are typing something that cannot be converted to an integer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Waria Ahmed: Please Use Code Tags; they make your posts much easier to read.

[edit]Add Please ! CR[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Thanks for the help...

One quick question...

Am i better off learning about collections and doing it that way? or should continue trying to work it through a basic array?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3673
    
  15
Originally posted by Waria Ahmed:
Thanks for the help...

One quick question...

Am i better off learning about collections and doing it that way? or should continue trying to work it through a basic array?


Collections are more flexible than arrays. Once created an array cannot be resized, other than by creating a new array and copying everything from the old one.
In some circumstances, arrays can be quicker than Collections for accessing items, although the opposite is also true in other circumstances.

Basically the answer is - it depends on what you plan to do with the items. Having said that, even if arrays are suitable for what you are doing, it's a good idea to learn about Collections so you can make an informed decision next time.
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
Ok, at the moment I am just trying to add a person manually to a person array.

The person array holds name, number and a Boolean for payment status.

I am going to simplify the code to just add the number for starters�

This is the code I am using, when I run it I get the NullPointerException..



What is the next step I need to take?

Thanks
Waria Ahmed
Ranch Hand

Joined: Jul 09, 2008
Posts: 56
If it helps, i also have the following code in my program which adds a person through hard coding, and this works...


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3673
    
  15
Originally posted by Waria Ahmed:
What is the next step I need to take?


Look at the stack trace from the exception. This will tell you which line of your code the exception occurred on. Check all variables on that line to see which one is null. Make it non null or put a check in to see if it is null before you dereference it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help adding people to array through a menu!!