title: How to search through a LinkedList for an object with a certain variable in Java?
body: <p>I have this issue where I can populate a LinkedList with objects which I can manipulate later in this shop system I am designing. The problem is that when I populate the list with more than one Object and then search for that object in the List; my program will tell me that the object I am looking for doesn't exist even though I can list the contents of the list and it will show. </p>
  
  <p>Any help would be appreciated.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Sales extends addVideoGame implements java.io.Serializable{
  
  private static LinkedList&lt;VideoGames&gt; sellGame = new LinkedList&lt;&gt;();
  private static String searchTerm;
  private static int searchQ;
  
  public static void sellItem(){
      sellGame = games;
      int listLength = sellGame.size();
      searchTerm = IBIO.inputString("What item would you like to sell: ");
      for(VideoGames v : sellGame){
      if(v.getTitle().contains(searchTerm)){
          IBIO.output("Item found: " + searchTerm);
          searchQ = v.getQuantity();
          IBIO.output("Available Quantity: " + searchQ);
          int sellQ = IBIO.inputInt("How much of this item would you like to sell: ");
          if(sellQ &gt; searchQ){
              IBIO.output("The amount you have specified is greater than the \ncurrent stock.");
              sellItem();
          } else {
              searchQ = searchQ - sellQ;
              v.setQuantity(searchQ);
              double sellP;
              sellP = sellQ * v.getPrice();
              IBIO.output("£"+sellP);
              String confirm = IBIO.input("This is the price you are selling these items for. Type 'Yes' to complete the order or 'No' to reject it. ");
              if(confirm.equalsIgnoreCase("Yes")){
                  IBIO.output("Order complete!");
                  try{
                      int receiptCount = 0;
                      PrintWriter receipt = new PrintWriter("C:\\Users\\Yemi\\Desktop\\TheStore\\receipt"+ receiptCount +".txt");
                      receipt.println("Item sold: " + v.getTitle());
                      receipt.println("Quantity sold: " + sellQ);
                      receipt.close();
                      receiptCount = receiptCount + 1;
                      IBIO.output("Receipt saved to: C:\\Users\\Yemi\\Desktop\\TheStore");
                  } catch(IOException io){
                      io.printStackTrace();
                  }
                  IBIO.output("Thank you for buying from Gamers Avenue UK!");
              } else if(confirm.equalsIgnoreCase("No") &amp;&amp; TheStore.privilege){
                  AccessMenus.adminMenu();
              } else {
                  AccessMenus.userMenu();
              }
              if(TheStore.privilege){
                  AccessMenus.adminMenu();
              } else {
                  AccessMenus.userMenu();
              }
          }
      } else {
          IBIO.output("The item you are looking for does not exist.");
          sellItem();
      }
      }
  }
  }
  </code></pre>
  
  <p>Here are the classes used to navigate the program if anyone needs them:</p>
  
  <pre><code>public class TheStore {
  
  static String password; //Variable created to hold and check the value of password against the correct value.
  public static boolean privilege = false; //Variable created to distinguish the difference between a normal user and a user with administrator privileges.
  
  public static void main(String[] args) {
      IBIO.output("Welcome to Gamers Avenue UK!");
      IBIO.output("Please make sure that you enter the correct password for your given privileges.");
      password = IBIO.inputString("Enter password: ");
      if(password.equalsIgnoreCase("admin")){ //Checks the entered value against the correct value.
          privilege = true; //Sets global boolean value to true, so that admin access is granted.
          IBIO.output(" ");
          AccessMenus.adminMenu();//If password is correct, loads admin menu.
      } else if(password.equalsIgnoreCase("user")){
          privilege = false; //Keeps admin access off, so that unauthorised changes cannot be made.
          IBIO.output(" ");
          AccessMenus.userMenu();//If correct, loads user menu.
      } else {
          IBIO.output("The password is incorrect. Exiting program."); 
          System.exit(1); //If an incorrect password is entered, the program will close.
      } //close else
  }//close main
  }//close class TheStore
  </code></pre>
  
  <p>Access Menus:</p>
  
  <pre><code>public class AccessMenus{
  
      public static int choice;//Variable which will hold the value, which corresponds to an action depending on what value is entered.
  
      public AccessMenus(){ //Null argument constructor, to set values to 0.
          AccessMenus.choice = 0;
      }
  
      public AccessMenus(int c){ //Single argument constructor.
          AccessMenus.choice = c;
      }
  
      public static void userMenu(){
          IBIO.output("1: Sell a product.");
          IBIO.output("2: Register a customer in the Loyalty programme.");
          IBIO.output("3: Stock check.");
          IBIO.output("4: Log out.");
          IBIO.output(" ");
          IBIO.output("Please make your choice: ");
          choice = IBIO.inputInt();
          if(choice == 1){
              Sales.sellItem();
          } else if(choice == 2){
              CustomerRandom.customerMenu();
          } else if(choice == 3){
              StockCheck.checkStock();
          } else if(choice == 4){
              IBIO.output("Logging out.");
              System.exit(1);
          } else {
              IBIO.output("Invalid choice. Returning to menu.");
              userMenu(); //If the value entered does not correspond to any action, the program will treat it as invalid and return to the menu.
          }//close else
      }//close userMenu
  
      public static void adminMenu(){
          IBIO.output("1: Sell a product.");
          IBIO.output("2: Go the Videogame management menu.");
          IBIO.output("3: Stock check.");
          IBIO.output("4: Register a customer in the Loyalty programme.");
          IBIO.output("5: Log out.");
          IBIO.output(" ");
          IBIO.output("Please make your choice: ");
          choice = IBIO.inputInt();
          if(choice == 1){
              Sales.sellItem();
          } else if(choice == 2){
              addVideoGame.vgMenu();
          }else if(choice == 3){
              StockCheck.checkStock();
          } else if(choice == 4){
              CustomerRandom.customerMenu();
          } else if(choice == 5){
              IBIO.output("Logging out.");
              System.exit(1);
          } else {
              IBIO.output("Invalid input. Returning to menu.");
              adminMenu();
          } //end else
      }//close AdminMenu
  }//close AccessMenus
  </code></pre>
  
  <p>This class is necessary as it allows you to populate the list: </p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class addVideoGame extends VideoGames implements java.io.Serializable{
  
  public static VideoGames game = new VideoGames();
  public static VideoGames eGame = new VideoGames();
  public static LinkedList &lt;VideoGames&gt; games = new LinkedList&lt;&gt;();
  public static LinkedList &lt;VideoGames&gt; loadList = new LinkedList&lt;&gt;();
  private static int vgChoice = 0;
  public static int vgCount = 0;
  public static int vgAmount = 0;
  
  public static void vgMenu(){
      IBIO.output("WARNING: USING OPTION 4 TO LOAD IN A LOCAL FILE WILL ERASE EVERYTHING CONTAINED IN THE CURRENT LIST. \nSave everything in the current list using option 3 before loading in data.");
      IBIO.output("1: Add a new videogame to the list.");
      IBIO.output("2: View the contents of the list.");
      IBIO.output("3: Save the contents of the list to the local area.");
      IBIO.output("4: Load in data from a local file.");
      IBIO.output("5: Return to the main menu.");
      vgChoice = IBIO.inputInt("Make your choice: ");
  
      if(vgChoice == 1){
          vgAmount = IBIO.inputInt("How many games would you like to add to the database?: ");
          for(int x = 0; x &lt; vgAmount; x = x + 1){
          VideoGames vg = new VideoGames(); 
          vg.setTitle(IBIO.inputString("Enter the title of the game: "));
          vg.setPublisher(IBIO.inputString("Enter the publisher of the game: "));
          vg.setDeveloper(IBIO.inputString("Enter the developer of the game: "));
          vg.setAgeRating(IBIO.inputInt("Enter the age rating of the game: "));
          vg.setGenre(IBIO.inputString("Enter the genre of the game: "));
          vg.setQuantity(IBIO.inputInt("Enter the available quantity of the game: "));
          vg.setPrice(IBIO.inputDouble("Enter the recommended retail price: "));
          game = vg;
          games.add(vg);
          IBIO.output(" ");
          }
          vgMenu();
      } else if(vgChoice == 2){
          IBIO.output("Current amount of games in the list: " + games.size());
              Iterator itr = games.iterator();
              while(itr.hasNext()){
                  Object g = itr.next();
                  IBIO.output(g + " ");
              }
              //System.out.println(Arrays.toString(games.toArray()));
          vgMenu();
      } else if(vgChoice == 3){
          try{
              ListIterator output = games.listIterator();
              while(output.hasNext()){
              Object o = output.next();
              FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
              ObjectOutputStream out = new ObjectOutputStream(fileOut);
              out.writeObject(game);
              out.close();
              fileOut.close();
              IBIO.output("Data saved to C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
              }
          } catch(IOException io){
                  io.printStackTrace();
              }
          vgMenu();
      } else if(vgChoice == 4){
          eGame = null;
          try{
              ListIterator input = games.listIterator();
              while(input.hasNext()){
              Object i = input.next();
              FileInputStream fileIn = new FileInputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
              ObjectInputStream in =  new ObjectInputStream(fileIn);
              eGame = (VideoGames) in.readObject();
              in.close();
              fileIn.close();
              games.clear();
              games.add(eGame);
              IBIO.output("Item added to list from local file.");
              }
          } catch (IOException i){
              i.printStackTrace();
              return;
          } catch(ClassNotFoundException c){
              IBIO.output("VideoGames class not found");
              c.printStackTrace();;
              return;
          }
          vgMenu();
      } else if(vgChoice == 5){
          IBIO.output("Returning to main menu: ");
          AccessMenus.adminMenu();
      } 
  }     
  }
  </code></pre>
  
  <p>Again, any help would be greatly appreciated!</p>