0
package sms;
 import java.io.*;
 import java.util.*;
 public class Student {
    PrintStream p=new PrintStream(System.out);
    Scanner sc = new Scanner(System.in);
    String reg;
    Student(){
        //p.println("Enter the name of the student:");
    }
    public void nstd() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> cse=new ArrayList<String>();
        ArrayList<String> ece=new ArrayList<String>();
        ArrayList<String> mec=new ArrayList<String>();
        ArrayList<String> eee=new ArrayList<String>();
        p.println("Enter the name of the student:");
        String name=br.readLine();
        p.println("Choose one group from fallowing:");
        String group=br.readLine();
        if(group=="cse"||group=="CSE"){
            cse.add(name);
            Acad name=new Acad();
        }
        if(group=="ece"||group=="ECE"){
            ece.add(name);
        }
        if(group=="MEC"||group=="mec"){
            mec.add(name);
        }
        if(group=="eee"||group=="EEE"){
            eee.add(name);
            }
    }
}

in the above code after adding an element to cse arraylist with the name provided i tried to create an object with the name but it shows duplicate variables found. how can i create an object with unique name for every student entry that can be with name or arraylist name fallowed by elements index (like cse0,cse1,etc..).

Vemuri Pavan
  • 495
  • 7
  • 15

2 Answers2

0

You can't declare variable names dynamically in Java. Variable name is a compile time thing. However you can dynamically refer to the declared variables at runtime through reflection.

But here in your case you should use array of objects(Acad) or List or Map of these objects to do so.

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
0

In Java you can't declare variable names dynamically. Java variable names are declared compile time not Run-time.

But from what I can understand from your question you can achieve this using a Map. Using a Map you can store an Object with a key of your definition.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Student {
    PrintStream p=new PrintStream(System.out);
    Scanner sc = new Scanner(System.in);
    String reg;
    Student(){
        //p.println("Enter the name of the student:");
    }

    public void nstd() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> cse=new ArrayList<String>();
        ArrayList<String> ece=new ArrayList<String>();
        ArrayList<String> mec=new ArrayList<String>();
        ArrayList<String> eee=new ArrayList<String>();
        HashMap<String, Acad> cseStudents= new HashMap<>();
        p.println("Enter the name of the student:");
        String name=br.readLine();
        p.println("Choose one group from fallowing:");
        String group=br.readLine();
        if(group.matches("^[Cc][Ss][Ee]$")){
            cse.add(name);
            cseStudents.put(name,new Acad());
        }
        if(group.matches("^[Ee][Cc][Ee]$")){
            ece.add(name);
        }
        if(group.matches("^[Mm][Ee][Cc]$")){
            mec.add(name);
        }
        if(group.matches("^[Ee]{3}$")){
            eee.add(name);
        }
    }
}

You can access the required object anytime using

Acad required=cseStudents.get(name);

There are few types of Map implementations that could be useful to you [HashMap][1], LinkedHashMap, TreeMap and Hashtable.

Each have there own advantages. Basically,

  1. HashMap

    This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. This implementation provides constant-time performance for the basic operations (get and put).

  2. LinkedHashMap

    This linked list maintains the order in which keys were inserted into the map (insertion-order).

  3. TreeMap

    The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

  4. Hashtable
    Almost similar to HashMap except for this is synchronized.