How to declare object reference variables and assign them values according to user input.?
<p>I'm creating a command line Movie Register where the user has different ways
to filter movies, directors and actors.</p>

<p>I'm wondering if it's possible to declare some object reference variables and assign them values according to the user input. That is, if the user input is lowercase I'd like to declare and assign value to a Person[] object, and and ArrayList (Persons) object etc, and if the user input is uppercase I'd like to declare and assign value to a Film[] object, ArrayList (Film) etc.</p>

<p>Code:</p>

<pre><code>//String kode is the user input
void find(String kode) {

if (Character.isLowerCase(kode.charAt(0))) { // if the input is a movie code

    ArrayList&lt;Person&gt; utvalg = new ArrayList&lt;Person&gt;();
    Iterator &lt;String&gt; it = personer.keySet().iterator();
    Person obj = new Person();
    HashMap &lt;String,Person&gt; map = Filmregister.personer;
    Person[] arr;

} else { // if the input is a person code

    ArrayList&lt;Film&gt; utvalg = new ArrayList&lt;Film&gt;(); 
    Iterator &lt;String&gt; it = filmer.keySet().iterator();
    Film obj = new Film();
    HashMap &lt;String,Film&gt; map = Filmregister.filmer;
    Film[] arr;

}

while (it.hasNext()) {

    String nokkel = (String) it.next();
    String subnokkel = nokkel.substring(0, kode.trim().length());

    if (subnokkel.equals(kode)) {

    obj = map.get(nokkel);

        utvalg.add(obj);

    } else {
    // do nothing
    } 
}

arr = Utils.sorterFilmerAbc(utvalg);

}
</code></pre>

<p>So, javac gives me "cannot find symbol" on all the variables when I reference them outside the if else blocks. I realise that it's because the variables are declared within the if else scope, but I was hoping there's a way to do this so I don't have to have if else statements everytime I want to reference either a Person object or a Film object.</p>

<p>I looked at the possibility to declare the list and maps with regards to Person and then cast the instances to Film objects in the if statement (if the user wants a list of films), but found that this type of casting requires that the classes have an inheritance relationship, and that is not suitable for these classes.</p>

<p>This is my first post, so if it's too trivial, please advise me.</p>
