0

I am not professional in java but I tried my best. My code reads a text file then it puts into an array then put each of its indexes into an array list but my problem I have to access an element of that array because my array looks like this (name,last name,quiz1,quiz2, midterm, project, final, average) Actually my ArrayLists first element like this so, for example, I tried to access midterm result and then print it to a table.

static List<Assign2> studentList = new ArrayList<>();
public static void main(String [] args) throws IOException{
        new myClass();


        File here = new File(".");
        System.out.println(here.getAbsolutePath());
        BufferedReader reader = new BufferedReader(new FileReader("A.txt"));
        String line= reader.readLine();
        String[] arr =line.split(",");


        Object[] data = {arr[0],arr[1],arr[2],arr[3]};
        //studentList.add(Arrays.asList(data));



    }
public static void table(List<myClass> b){
            System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s", "NAME","LAST NAME","ID","QUIZ1","QUIZ2","PROJECT","MIDTERM","FINAL","AVERAGE","LETTER GRADE");
            System.out.println();
            System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            for(int i=0;i<b.size();i++){
                System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s",b.get(i).getName(),b.get(i).getLastName(),b.get(i).getID(),b.get(i).getQuiz1(),b.get(i).getQuiz2(),b.get(i).getProject(),b.get(i).getMidterm(),b.get(i).getFinalGrade(),b.get(i).getAverage(),b.get(i).getLetterGrade());
                System.out.println();
            }
        }
Ekin
  • 37
  • 1
  • 2
  • 9
  • Possible duplicate of [Java ArrayList Index](https://stackoverflow.com/questions/4313457/java-arraylist-index) –  Nov 24 '17 at 18:58
  • no mine is not duplicate because in those examples they just choose integer,string but mine is get my classes name because it has both integers and strings @Artemis – Ekin Nov 24 '17 at 19:01
  • Object[] data = {arr[0],arr[1],arr[2],arr[3]}; Object receiver = data[0]; this? – Jhonatan S. Souza Nov 24 '17 at 19:01
  • 1
    If your file content is data then you can define class that represents the data and have Collection of that type and make use of [Java Stream API](https://stackoverflow.com/questions/31381992/java-equivalent-of-where-clause-in-c-sharp-linq) – ChiragMM Nov 24 '17 at 19:08
  • The whole thing is a mess, the idea, the process, the code, the answers too. You have [multiple XY problems](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) nested one into the other. Restart from scratch, with a clear idea, then split every problem into an atomic step, solve the problem, move to the next step, and the result will be the sum of lots of small problems solved, which will end in understandable, working code – Andrea Ligios Nov 24 '17 at 19:12

2 Answers2

1

You should do like this:

List<myClass> studentList = new ArrayList<>();

myClass obj=new myClass();

studentList.Add(obj);

...

for(int i=0;i<studentList.Count;i++)
{
  myClass studentObject=studentList[index];
  // do staff
}
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
1

Isn’t it easier to create a class that saves your variables (name, last name etc.) and then creat an ArrayList of that class type (ArrayList) and call the desired item and access the data you need

alex_z
  • 416
  • 5
  • 12