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();
            }
        }
5
  • Possible duplicate of Java ArrayList Index Commented Nov 24, 2017 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 Commented Nov 24, 2017 at 19:01
  • Object[] data = {arr[0],arr[1],arr[2],arr[3]}; Object receiver = data[0]; this? Commented Nov 24, 2017 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 Commented Nov 24, 2017 at 19:08
  • The whole thing is a mess, the idea, the process, the code, the answers too. You have multiple XY problems 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 Commented Nov 24, 2017 at 19:12

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

For sure! i think is the best way.
@Lioxa what you mean?huh? you are definitely incorrect
@Mr.AF my method does not have 2 dimensional Array. Its easier to creat a class with the desired variables, save the info to the class and add it into the ArrayList of the class type.
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
}

4 Comments

no my arraylist is like this static List<myClass> studentList = new ArrayList<>();
for the index part should I write a for loop right?
@Ekin get your list items count and depend on count create a for loop
but in the loop it can not print it out it takes nothing because in your code there is no data array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.