Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help adding people to array through a menu!!

 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic