• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Collection not working, Help needed  RSS feed

 
Indranil Sinha
Greenhorn
Posts: 20
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am working on my beginners project in java.
The project is to a add, list and delete person. like

type 1 to add a person
type 2 to list all person
type 3 to delete a person.

I am using ArrayList collections to work on this project.

The first question is is using the collections is a good aproach?
if yes i am declaring the ArrayList globally but while iterating it I can see it's not storing any data.

my code is as follows.



first output is

Enter 1 for Adding new Record
Enter 2 to see List of Records
Enter 3 to Delete a record
Enter 4 to Exit!!

while entering 1 its coming like this

Enter 1 for Adding new Record
Enter 2 to see List of Records
Enter 3 to Delete a record
Enter 4 to Exit!!
1
Enter a the id
1
Enter a the First Name
Indranil
Enter a the Last Name
Sinha

Then after hitting enter again the first four options are coming and after going to 2 its not showing anything.

Enter 1 for Adding new Record
Enter 2 to see List of Records
Enter 3 to Delete a record
Enter 4 to Exit!!
1
Enter a the id
1
Enter a the First Name
asdasd
Enter a the Last Name
dsffdgdfg
Enter 1 for Adding new Record
Enter 2 to see List of Records
Enter 3 to Delete a record
Enter 4 to Exit!!
2
Enter 1 for Adding new Record
Enter 2 to see List of Records
Enter 3 to Delete a record
Enter 4 to Exit!!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you're creating new instances of OperationClass every time you want to call one of its methods, instead of using just a single instance. Remove the "new OperationClass()." from everywhere but line 10, and see what happens.
 
Indranil Sinha
Greenhorn
Posts: 20
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The problem is that you're creating new instances of OperationClass every time you want to call one of its methods, instead of using just a single instance. Remove the "new OperationClass()." from everywhere but line 10, and see what happens.

Thanks...
 
himashree rawat
Greenhorn
Posts: 9
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Everytime new instance creation(that is why added data is vanished) is a problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!