• 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

Submenu in switch case, (String header) error

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My task is to create menu where I can add item to array list, search, modify or display, save/load.
here is my menu class, problem that I am facing here is : 1)  I am not sure how can I correctly make a submenu, or even can I do it the way I did... 2) print method does not work for me. Error: method print String is not applicable for the argument.



Would like to ask for help.
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to mention: I can not add new item to arrayList, seems that nothing is working because of this submenu, previously with single menu everything was working
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling your print() method with no arguments but you have implemented it as taking a String argument.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vadim Melders wrote:... here is my menu class, problem that I am facing here is : ... 2) print method does not work for me. Error: method print String is not applicable for the argument.


I'm guessing this is line 60 in the Warehouse_menu class... but I shouldn't have to guess.  Always include the full error message with the stack trace.

In this line in the Warehouse_menu class, you call the print() method on a variable of type Gpus.  But in Gpus, you declare the print(String) method like this:
Do you see the problem?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here is my menu class, problem that I am facing here is : 1)  I am not sure how can I correctly make a submenu


Couldn't you create a method and call it from the case in the switch? [Edit] Ah, I see that is what you're doing already.  Could you describe in detail what is going wrong?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vadim Melders wrote:Forgot to mention: I can not add new item to arrayList, seems that nothing is working because of this submenu, previously with single menu everything was working


I line 38, you are declaring the gpus object in the submenu.  As soon as you return from the submenu, gpus goes out of scope.  Try making gpus an instance variable by declaring it at the top of the class outside of any methods.
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:
I line 38, you are declaring the gpus object in the submenu.  As soon as you return from the submenu, gpus goes out of scope.  Try making gpus an instance variable by declaring it at the top of the class outside of any methods.


Thank you. Me being silly, now item can be added to array.

public void print(String header) - String header - I always leave it empty, that comes from the teacher, I can not get the purpose of having String header there. So when I declaring String obviously Integer can not be applicable... Also I had String header in this part of code. But I will remove it from both classes.

Knute Snortum wrote:Could you describe in detail what is going wrong?


I wasnt sure if it is correct way of making submenu by just calling a class from option menu.
Now adding method is working because array variables declared static outside the classes, problem is when submenu case breaks menu jumps to main switch case menu options, how can I make it stay on the same menu until I press back to jump to main one.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wasnt sure if it is correct way of making submenu by just calling a class from option menu.  


You don't call classes, you call methods in a class.  This may sound nitpicky, but it's important.

...problem is when submenu case breaks menu jumps to main switch case menu options, how can I make it stay on the same menu until I press back to jump to main one.


If I'm understanding you, the submenu should return to the place it was called and continue to the next line.  Is this not what you're seeing?  Can you post the code that's not working as you expect?

Or, even better, write a SSCCE (that's a link) to test what you want to do.  If things still don't work correctly, you can at least post the SSCE.  If they do work correctly, you can apply what you've learned to the original code.

 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like I got all methods working - save, load, find, print. But I have new challenge, I would like to ask for help - how do I remove unwanted item from array list. For example I can find item with reference number code :

I would like to make same method to find via reference but also I want to remove particular item from the array list.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to remove items you will need an index, not a reference. So copy your find() method to a remove() method and once it finds the index then use the index to remove the item from both array lists.
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created remove method, copied find() to that

How do I remove found item?
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

With this code it doesnt remove item with reference 004, it removes 4th item e.g.

after use

then I do 004 once again

and 003 goes off, so it removes items by array position not by reference
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Troubles : now my adding method broke down, it rewrites last input for 2 array items ... Im dead X(
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two lists that you need to keep in sync (not a good design decision), so if you delete an item from one then the other must be deleted as well. This is where using an index is necessary over using a [Java] reference.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your remove() method shouldn't be returning a [Java] reference, if anything, it should return a boolean indicating if the removal was successful.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you ever used Maps? Because of the potential for syncing problems between two lists, one of which contains only keys, this seems like a perfect place to use a Map. If you added your key as a field to your Gcard class and then created a Map like
With String being the key and Gcard being a Java reference to an instance of the Gcard class then adding a new card is trivial.
And to find it
Using Object I/O becomes simple as well, just write/read the map.
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, haven't used it before. I am thinking to write everything from scratch again because I don't know what I did but I can not add items to array list like before, previous items in the list get replaced with new input.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are considering a re-write, you might want to look at SSCCE (<-link). If you can trim down you Gcard class, and trim down the Gpus class  (e.g. no file I/O), and instead of having a menu hard code the steps you want to test, you'd end up with an SSCCE. The advantage of an SSCCE is that you can post it and anyone here can cut and paste your code so that they can reproduce the error as well. This helps you to isolate the problem and helps us help you.

If you aren't going to try Maps then here's another way to eliminate your parallel lists. Add a "key" field to your Gcard class, and eliminate your keys list. This means that you can't use the built in indexOf to find the item but it's easy enough to brute force.



 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your advice, I will try to do it without keys today.
Will my save/load method in gpus class work without it? Because that's the only method of loading/saving I know from the uni guide
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your save/load of gpus should still work. I might suggest using try-with-resources to clean things up a bit. Using try-with-resources automatically handles the closing of any declared resources. You can do the same thing with your load method.

 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Add a "key" field to your Gcard class

- from what I found about field - A Field provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field. But no idea how I put it to the code.
when right-click "create field object for gpus" then - private Object gpus; appears, but methods like .size .get .remove - are not defined for the object, is that means that I should move those methods from Gpus class to Gcard? Sorry for question bombardment
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should be
just like you had before
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing because you have not provided the code for this class.
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried my best - no success. Gcard

Gpus

and main
 
Vadim Melders
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my first attempt I could add gcard to gpus array, I was able to save them and items also was loading on start and I could print them out - that what basically my assignment is asking me to do. Then I tried to implement remove method and here is where all my troubles began and I am back to 0... Nothing is working.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic