• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arrayList display method not functioning

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends, I am having a bit of an issue with my code here, the problem I am attempting to resolve is the listUsers method in the Facebook class (lines 26-29 in the Facebook class  ) If I am to input 1 for a menu choice, I am supposed to run the listUsers method displaying all of the users in my users ArrayList. However, nothing is happening when the listUsers method runs, it simply takes me back to the menu?
Any ideas on what may be causing this issue?




 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello Friends, I am having a bit of an issue with my code here, the problem I am attempting to resolve is the listUsers method in the Facebook class (lines 26-29 in the Facebook class  ) If I am to input 1 for a menu choice, I am supposed to run the listUsers method displaying all of the users in my users ArrayList. However, nothing is happening when the listUsers method runs, it simply takes me back to the menu? Any ideas on what may be causing this issue?



There was no issue when I run the program. The following code prints the entered users.



   
 
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess that there are no users in your List.  Write a test class to verify your Facebook class is behaving as expected.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realize now the list displays so long as I don't exit the program.., I was creating users,  then quitting the program, so that the data would seriialize, however when I start a new program, no users are listed, any idea why this may be ?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:I realize now the list displays so long as I don't exit the program.., I was creating users,  then quitting the program, so that the data would seriialize, however when I start a new program, no users are listed, any idea why this may be ?



The program needs to save the entered data, and I think it is trying to attempt something like that while a user quits the program (menu option 5). And I see there is an exception then on my pc:



The program need to create the data file and write to it and read from it when needed. The program can do this:
- create a data file when the program starts for the first time.
- write data to the file when new users are entered.
- read the file when the program is started (so that the users are in the memory).

I see some code in the quit() method about writing to a file. But, you need to test that properly  and see if its really doing what it is supposed to do.


 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have updated the listUsers method, however it is still not functioning properly after I close the program and re open, no users are listed.
 
Ron McLeod
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:I have updated the listUsers method, however it is still not functioning properly after I close the program and re open, no users are listed.


Can you explain how storing and retrieving user data works in your application?
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The users are added and deleted via the corresponding menu methods,
once the quit method is selected the program then creates a file and object output stream, serializing the data to the facebookUsers.dat file.
Once the program runs again, and I select list users method, the program is to then create an objectInputStream, at which time it reads the arrayList to the console from the facebookUser.dat file
the last process is where I am having the issue
hope this clarifies
 
Ron McLeod
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:... Once the program runs again, and I select list users method, the program is to then create an objectInputStream, at which time it reads the arrayList to the console from the facebookUser.dat file


I don't see anywhere in the code that the input stream is being read; also nothing being added to the ArrayList of users.
I reformatted the code to make it easier to read
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly, that is what I do not know how to do. Do you have any idea how to do this?
 
Ron McLeod
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are on  the right track by creating an ObjectInputStream to deserialize your saved data, but you never actually do anything with it. You will want to:
    - read an object from the stream
    - verify that the object is a FacebookUser type
    - add the user to the list

Also, loading stored users should be separated from displaying the users list.  You don't want to keep adding the saved users to your list each time it is displayed.

For example:FacebookUser and UserAccount need to implement Serializable, Facebook doesn't.

I would also rename the quit method to something like saveUsers, since that is what it is actually doing.

Finally, write a simple program to unit test your implementation outside of the full application.  For example:

 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my solution, thanks for everyones help, the program still has a way to go but it's getting their,
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the start of the application the loadUsers() method throws an exception: java.io.FileNotFoundException:
C:\Users\...\JAVA \StompsNoah_Assignment_03FacebookUsers.dat (The system cannot find the path specified)
       at java.io.FileInputStream.open0(Native Method)
       at java.io.FileInputStream.open(Unknown Source)
       at java.io.FileInputStream.<init>(Unknown Source)
       at java.io.FileInputStream.<init>(Unknown Source)
       at Facebook.loadUsers(Facebook.java:34)
       at Facebook.main(Facebook.java:169)


See this link refers to file related API: https://docs.oracle.com/javase/7/docs/api/java/io/File.html . Here one can find information and file related methods to create a file object and verify if the file exists or not. This may be used to check if the file exists at the program startup and act accordingly.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative would be to use a file chooser. Hint: if you are using a file chooser from the command line, you may need to pass null as the argument to the show XXX dialogue methods.
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic