title: How Do I Convert A String To A Generic Type?
body: <p>I am meant to create a class that reads a text document with employee information (ID number, name, and email) and convert the information into a Map which contains two components: a string with the ID number, and an Employee object with the name and email, separated by a single space (as opposed to the two spaces in the text document). I (almost) solved this by creating a program which successfully iterates through the text document, dividing each line into three parts, divided by each set of two consecutive spaces, then adding the last two parts together. However, as the last part is in string format at this point, I need to convert it into an Employee object before I can insert it into the &lt;String,Employee&gt; HashMap, but all the research I've done before coming here says that one cannot cast a string to a generic, and I can't think of any other way to do this.</p>
  <p>Here is the class-provided (uneditable) main class I'm meant to conform to:</p>
  <pre><code>public static void main(String[] args)
    {
      Map&lt;String, Employee&gt; data = Employee.load();
      Scanner keyboard = new Scanner(System.in);
      System.out.println(&quot;Enter ID of employee you're looking for&quot;);
      String id = keyboard.nextLine();
      System.out.println(data.get(id));
    }
  </code></pre>
  <p>and here is the class I've made to try to create the correct HashMap to return:</p>
  <pre><code>public static Map&lt;String,Employee&gt; load() {
          File f=new File(&quot;employees.txt&quot;);
          Map&lt;String,Employee&gt; result=new LinkedHashMap&lt;&gt;();
          String ID = null;
          String name = null;
          String email = null;
          String b;
          try {
              BufferedReader in = new BufferedReader(new FileReader(f));
              String line;
              String[] values;
              int i=0;
              while(((line=in.readLine())!=null)){
                  line=in.readLine();
                  values= line.split(&quot;  &quot;);
                  if (i&gt;0){
                  //this is meant to ignore the first line, as it contains irrelevant information
                  ID=values[0];
                  name=  values[1];
                  email=values[2];}
                  i++;
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          b=name+&quot; &quot;+email;
          result.put(ID, (Employee) b); //&quot;(Employee) b&quot; has an error that reads:  
          //Inconvertible types; cannot cast 'java.lang.String' to 'Employee'
          return result;
      }
  }
  </code></pre>