Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: List that generates new item variables?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default List that generates new item variables?

    Hi all,

    I'm very very new to Java and am trying to create a simple inventory management system. My question may be answered somewhere else but I don't know what search terms to use. In trying to find solutions I kept getting referring to ArrayList but I don't think that this is what I'm looking for.

    What I'm trying to do right now is to create a list based off of user input in a do while loop (current code below). So, rather than declare Item1, Item2, Item3, etc... I would like this loop to ask the user to enter an item and then ask them if they want to enter another item, if they do want to enter another item I would like java to create the variable for me and name it Item2.

    I would think there is a simple way to achieve this bu I wouldn't know where to start. Any ideas?


        public static void main(String[] args) {
            Scanner user_input = new Scanner(System.in);
            String Continue;
            do{
                String Item;
                System.out.print("Enter Item Name:\t");
                Item = user_input.next();
     
     
                System.out.print("Continue? Y/N:\t");
                Continue = user_input.next();   
     
        } while("Y".equalsIgnoreCase(Continue));
        } 
     
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: List that generates new item variables?

    If I understand your problem correctly, using an ArrayList is correct:
    List<String> items = new ArrayList<String>();
    do {
        String Item;
        System.out.print("Enter Item Name:\t");
        Item = user_input.next();
        items.add(Item);
        ...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List that generates new item variables?

    Hi aussiemcgr thank you for your input. The examples of ArrayList I found online showed "items.add(Item);" as things that were manually entered. Will ArrayList create new items or do they need to be manually entered?

    I don't understand how ArrayList works (obviously lol) but I'm playing with what you provided to see if it is what I want. I'm getting an error for the first line "cannot find symbol". Do I need to create a List class? or is this the proper syntax? ArrayList<String> items = new ArrayList<>();

    --- Update ---

    UPDATE:

    Well I got a chance to talk to my Java teacher about it and he said to wait until we get into using a database. Apparently doing it this way (still not sure how it works) will only be temporary. If I populate the list then close the program and come back to it, I'll have to populate the list again. So if the good people of this forum think I should wait as well, then I guess I'll wait. However, if you could point me in the right direction that would be pretty cool too.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: List that generates new item variables?

    There's nothing wrong with using an ArrayList now and modifying your code to use a database later. Start with what you know or can reasonably figure out. The contents of the ArrayList can be stored to a file on your hard drive a number of different ways, so it's not necessarily true that the ArrayList will have to be repopulated every time you return to the program.
    Will ArrayList create new items or do they need to be manually entered?
    Not sure what you mean by this. You may be asking if the ArrayList will automatically grow to accommodate the number of items needed. If so, yes, it will.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List that generates new item variables?

    Further Update:

    So after playing with the ArrayList a bit more I realized that it is doing what I want, kind of. The list only seems to exist while I run the program and once it stops the list is nonexistent (this is what my teacher was telling me).

    Below is basically what I need from my program but as I'm writing it out it seems that it's all kind of intertwined so if this violates a rule of the forums or is too much to receive a response on then I understand but I'm going to leave it here in case it is acceptable and in case someone feels like giving me some generic places to start.

    In addition to the list being created and not de-populate after every run, I need some way to assign default values to each item. So for example if the first item is "Apples" then I need to be able to state that there are x amount of apples. So I think I need each list item to be converted or set to an int (based on user input). I also need a way for the user to say when they have used an item. So after the list has been created and default values assigned I need to have a user input option something like the code below to ask if they've used an item, ask which item if they have used an item, and how much of the item has been used. However, I need their response to update the previously created and stored item's information. So using apple again, if "Apples" was originally stated as having ten apples and the user used 3 I need the responses from the code below to update the code for "Apples" to say that there are 7 apples. None of this seems terribly complicated but because I don't know jack about java or what terms to use to search for solutions, I'm pretty stuck.

    String usedItem;
            System.out.println("Have you used an item? Y/N");
            usedItem = user_input.next();
            while (usedItem.equalsIgnoreCase("Y")){
            do{
                String itemUsed;
                System.out.print("Enter Used Item Name:\t");
                itemUsed = user_input.next();
     
                int amountUsed;
                System.out.print("Enter amount of" + itemUsed + "used:");
                amountUsed = user_input.nextInt();
     
                System.out.print("Have you used an item? Y/N:\t");
                usedItem = user_input.next(); 
     
                System.out.println(items);
     
        } while(usedItem.equalsIgnoreCase("Y"));


    --- Update ---

    Hi Greg,

    Thanks for your response. My response below yours is something I was typing out while I came up with the code displayed so that's why I didn't address your comment in mine.

    I think with some research I can probably find how to store ArrayList contents to a file so, I'll work on that some time this weekend but if you want to give instructions or a resource I won't object
    Last edited by GregBrannon; October 3rd, 2014 at 01:34 PM. Reason: Fixed code tags: don't capitalize them.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: List that generates new item variables?

    Do you know OOP? An ArrayList can be used to store objects, instances of a class. You might have an Apple class and a Basket class, something to hold the Apples, whatever you want to call it. The Basket could hold Apple objects and keep track of the details you've described as fields, and the Basket objects could be stored in the ArrayList.

    As for restoring the list each time the program is started, that could be done automatically if the data has been stored in a file.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List that generates new item variables?

    I think I've sort of been introduced to OOP but don't fully understand it. I think one of the things you're suggesting is to have the user declare the class and the initial amount at the same time with something like the code below. However, the way I'm doing it isn't working. I'm getting the error below. I think it's due to my use of Item in the items.add line. Not sure how to resolve it though. Or if this is even the right approach.

    Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at kitchen.management.KitchenManagement.Item(KitchenM anagement.java:64)
    at kitchen.management.KitchenManagement.main(KitchenM anagement.java:33)
    Java Result: 1

                String Item;
                String itemAmount;
                System.out.print("Enter Item Name:\t");
                Item = user_input.next();
                System.out.print("Enter Item Amount:\t");
                itemAmount = user_input.next();
                items.add(Item(itemAmount));
     
                System.out.print("Continue? Y/N:\t");
                Continue = user_input.next();   
     
                System.out.println(items);

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: List that generates new item variables?

    Ya, you have anticipated some basic OOP concepts, but you're jumping into the deep end before you're ready. Why is that? What's the hurry? Can't you learn how to use these tools before building something?

  9. #9
    Junior Member
    Join Date
    Oct 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: List that generates new item variables?

    I suppose I am getting a bit ahead of myself. I've learned that I'm more of a hands on person than I used to think though. I can study history and psychology and retain the knowledge and what not just fine but when it comes to concepts like this or even math, I do much better if I can experiment with it or at least type/write it out to get a better feel of it. But when the teacher talks about it in class, I pay attention but because I haven't experienced it yet, it kind of goes over my head. We'll probably touch on the necessities for this in more detail but I wanted to see if I could build out this idea.

    I think another part of it is that there seems to be so many different ways to do everything. So the teacher can give an example in class but that's just one way to use it. I don't know, if what I'm asking is too much or not appropriate for whatever reason I can wait until I learn more from my class. I just kind of dive into things that I'm interested in and this happens to be something I'm interested in.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: List that generates new item variables?

    You can work ahead, but there's value in learning concepts in the right order and practicing them until they're understood before moving to the next.

Similar Threads

  1. [SOLVED] Random 4 Char Generator Generates 3 Numbers Instead?
    By BenjaminJ in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 10th, 2014, 08:19 AM
  2. display item for item selected in combobox
    By cisco.nat in forum AWT / Java Swing
    Replies: 1
    Last Post: June 20th, 2013, 07:12 AM
  3. Replies: 1
    Last Post: March 21st, 2013, 08:51 AM
  4. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM