Pregunta

I need to create an ArrayList (well it doesn't have to be an ArrayList but that's what I'm attempting so far) to hold student ids, marks, and course names. I have something like this so far:

import java.util.*;

public class MarksHandling {
  static Scanner keybrd = new Scanner(System.in);

  public static void main (String[]args){
    ArrayList<String> studentsCourses= new ArrayList<String>();
    ArrayList marks= new ArrayList();

while (keybrd.next()!="-1"|| keybrd.nextInt()!=-1){
  System.out.println("Please Type Your ID Number (-1 to quit)\n");
  studentsCourses.add(keybrd.next());
  System.out.println("Please Type Your Course Name (-1 to quit)\n");
  studentsCourses.add(keybrd.next());
  System.out.println("Please Type Your Mark (-1 to quit)\n");
  marks.add(keybrd.nextInt());
    }
  System.out.println("Done");

  }
}

(Mind the indentation; it tends to be messed up when I paste in from my IDE) I understand that I could just enter all of them as strings and it would be find; but in the end I need to find which student has the highest grade and in which course...

So I thought of trying two different arrays (One for Strings and one for Int) but that caused issues with my sentinel (Which I am currently trying to fix..)

Is there a way to accept both "int" and "String" types into the same arrayList and access them as such?

¿Fue útil?

Solución

Better way to do this in a OO language is to create a Student class with id, marks, and course names as member variables of this class, and to use

ArrayList<Student> studentsCourses= new ArrayList<Student>();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top