0

I have a 2D array called gradeTable. This array contains grades that fall into the categories of a, q, p, e, and f. I'm trying to create a method that takes in a new grade (i.e. a100, q90, e65) and adds that to the appropriate row. The grade is taken in as a string with the category following the numerical grade. I know I need to create a new array every time a new grade is added, but I'm not sure how to get each grade in it's appropriate spot. The method is a boolean and returns true if the grade is added and false if the category (the letter at the beginning of the string) isn't found. Any advice would be appreciated.

public boolean addGrade(String newGradeIn) {     

  char row = newGradeIn.charAt(0);
  int grade = Integer.parseInt(newGradeIn.substring(1));

  gradeTable[row] = Arrays.copyOf(gradeTable[row], gradeTable[row].length + 1);    

 }

That's all I have so far. I know it doesn't compile because I don't have a return statement.

3
  • 1
    Try ArrayList instead of array, to avoid having to create a new array every time Commented Oct 20, 2014 at 16:27
  • I need to use an array. Commented Oct 20, 2014 at 16:27
  • 1
    What do you have so far? Commented Oct 20, 2014 at 16:28

1 Answer 1

2
String input = /*get user input*/
char cat = input.charAt(0);
int grade = Integer.parseInt(input.substring(1));

int[] oldArr = gradeTable[cat];
int[] newArr = Arrays.copyOf(oldArr, oldArr.length + 1);
newArr[newArr.length - 1] = grade;
gradeTable[cat] = newArr;
Sign up to request clarification or add additional context in comments.

2 Comments

@kb94 You are going to have to put in a little legwork yourself here. Do you not understand the code I wrote above? If not, is there something specific you do not understand?
I don't know much about arrays, and I'm having trouble grasping all of the concepts. I understand the code now. Thanks for your help. I still don't understand how to get the method to return true if a new grade was added and false if it was not.

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.