-2

I have two classes. At the moment, you can save 99 entries to a .txt file. Obviously I want to expand upon this. It would be good if the following from ExamGradesGUI were an arraylist rather than an array:

    String[] firstName = new String[99];
    String[] lastName = new String[99];
    String[] subjectUnit = new String[99];
    double[] examMark = new double[99];

I managed to start off by declaring the array as below (for firstName):

ArrayList<String> firstName = new ArrayList<String>();

Then, I was not sure how to make it work with my get and set methods as obviously they are still in array form. If someone could help I would appreciate it. Thanks

ace96
  • 1
  • 2
  • 1
    You should always code to the interface. Don't declare variables of concrete collection types. Instead of `ArrayList firstName = new ArrayList();` you need to do `List firstName = new ArrayList();` – malfunctioning Apr 29 '15 at 09:59
  • `Obviously I want to expand upon this, obviously they are still in array form`, you are in for a surprise. The solution is obvious as well. – Chetan Kinger Apr 29 '15 at 09:59
  • You can use the `get(index)` resp. `set(index, element)` methods on the list. But you need to create the list as `List firstName = new ArrayList(99);` – SubOptimal Apr 29 '15 at 10:00
  • possible duplicate of [how to use an array list?](http://stackoverflow.com/questions/2697182/how-to-use-an-array-list) – Chetan Kinger Apr 29 '15 at 10:02
  • @malfunctioning in the getText() field firstName[i] = firstNameTxt.getText(); the expression must be an array type but is resolved to Array – ace96 Apr 29 '15 at 10:05

2 Answers2

1

Maybe it should be more OOP to have :

List<Student> students = new ArrayList<Student>();

Class Student{
    private String firstName;
    private String lastName;
    private String subjectUnit;
    private double examMark ;
    // Generate getters and setters and constructor

}

// So the code to create a new Student in getText(): 
 Student newStudent = new Student ( 
       firstNameTxt.getText() ,
       lastNameTxt.getText(), 
    subjectUnitTxt.getItemAt(subjectUnitTxt.getSelectedIndex()), 
    Double.parseDouble(examMarkTxt.getText()) );

// add the newStudent to the list
students.add( newStudent );
Pierre Jean
  • 366
  • 2
  • 9
  • This is good, but how would it work with: public ExamGradesGUI readRecords(){ in the ExamGrades program? – ace96 Apr 29 '15 at 10:12
0

There is a method List#add(int index,E element)

void add(int index,
       E element)

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

with this method you can replace

firstName[i] = firstNameTxt.getText();

this with

firstName.add(i,firstNameTxt.getText());

Update

you can replace

public void addRecords(String[] first, String[] last, String[] subject, double[] mark) 

with

  public void addRecords(List<String> first,List<String> last, List<String> subject,List<double> mark) {
this.firstNameList=first;
this.lastNameList=last;
this.subjectList=subject;
this.marksList=mark;
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Thanks, now for the line: save.addRecords(firstName, lastName, subjectUnit, examMark); "The method addRecords(String[], String[], String[], double[]) in the type ExamGrades is not applicable for the arguments (ArrayList, String[], String[], double[])" What would I change: public void addRecords(String[] first, String[] last, String[] subject, double[] mark) to? – ace96 Apr 29 '15 at 10:09
  • It won't work for this line: gui.firstName[i] = input.next(); the type of the expression must be an array type but it is resolved to a list. It says dimensions are expected after this token for the one as well – ace96 Apr 29 '15 at 10:30
  • We are not here to write code for you , this site is just for guiding you on correct path , so that you can resolve it on your own . You get the idea now just go ahead and try to fix it yourself !! **P.S. : Don't just copy and paste the Code** – Neeraj Jain Apr 29 '15 at 10:37