Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Difficulties with Arraylists

$
0
0
Greetings all,

I have recently been trying to wrap my head around Arraylists as well as For-Each loops. I've got the basic concepts down (I believe), but while I was writing out a simple employee management program to get the general idea into practice, I ran into a few speed bumps. Basically what i'm trying to do is create a basic console program that allows the user to input new employees and view existing employees which are all stored in an ArrayList. However when i feed my ArrayList of type Employee into my findEmployee method and use a for-each to search through it, it gives me an error stating that the ArrayList is full of generic objects and that the Employee type is incompatible. However when i change the search type to Object, it can no longer find the methods that contain all of the employee information, located inside the Employee class.

Below is the class with the error. There is a second class titled Employee as mentioned above, however it only has a few get and set methods for now in order to store employee information so i will not be posting that.

import java.util.ArrayList;
import java.util.Scanner;

public class ForEachPractice
{ 
/* MAIN METHOD */
  public static void main(String[] args)
  {
    ArrayList<Employee> employees = new ArrayList<Employee>();
    Scanner input = new Scanner(System.in);
    int decision = 0;
    boolean terminate = false;
    while(terminate == false)
    {
      System.out.println("Please choose one of the following options(enter the corresponding number for the operation or anything else to end the program):");
      System.out.println("1) Add Employee");
      System.out.println("2) Find Employee");
      decision = input.nextInt();
      
      if(decision == 1)
      {
        employees.add(addEmployee());
      }
      else if(decision == 2)
      {
        findEmployee(employees);
      }
      else
      {
        terminate = true; 
      }
    }
  }
/* ADDEMPLOYEE METHOD */
  public static Employee addEmployee()
  {
    Scanner input = new Scanner(System.in);
    Employee instance = new Employee();
    
    System.out.println("Please enter the user's first name.");
    instance.setFirstName(input.next());
    System.out.println("Please enter the user's Last name.");
    instance.setLastName(input.next());
    System.out.println("Please entert the user's Salary.");
    instance.setSalary(input.nextFloat());
    
    return instance;
    
  }
/* FINDEMPLOYEE METHOD */
  public static void findEmployee(ArrayList a)
  {
    Scanner input = new Scanner(System.in);
    int decision;
    String tempNameStorage;
    float tempSalaryStorage;
    
    System.out.println("Please select the Method of finding the Employee(enter the corresponding number)");
    System.out.println("1) First name");
    System.out.println("2) Last name");
    System.out.println("3) Salary");
    decision = input.nextInt();
    
    if( decision == 1 )
    {
      System.out.println("Please enter the Employee's first name (capitalize only the first letter)");
      tempNameStorage = input.next();
      for(Object e : a)
      {
        if(e.getFirstName() == tempNameStorage)
        {
          System.out.println(e.getFirstName() + e.getLastName() + " -- " + e.getSalary());
        }
      }
    }
    else if( decision == 2 )
    {
      System.out.println("Please enter the Employee's Last name (capitalize only the first letter)");
      tempNameStorage = input.next();
      for(Employee e : a)
      {
        if(e.getLastName() == tempNameStorage)
        {
          System.out.println(e.getFirstName() + e.getLastName() + " -- " + e.getSalary());
        }
      }
    }
    else if( decision == 3 )
    {
      System.out.println("Please enter the Employee's salary");
      tempSalaryStorage = input.nextFloat();
      for(Employee e : a)
      {
        if(e.getSalary() == tempSalaryStorage)
        {
          System.out.println(e.getFirstName() + e.getLastName() + " -- " + e.getSalary());
        }
      }
    }
    else
    {
      System.out.println("Sorry, that was an invalid entry.");
    }
  }
}


Any assistance with this issue would be greatly appreciated!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>