954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Two Dimensional Array Problem

Hey Guys,

I am a new student to Java and I am working on an assignment for school. I am not looking for anyone to do my work for me, just some guidance on what I can do to fix the problem. I have done research on this issue on google and visited various sites without getting the information I am looking for. I have contacted my professor as well, but he is usually very slow at responding to my questions. I am running out of time for this assignment and I would like to fix this bug before submitting the assignment.

Okay, now that the formalities are out of the way, here is the code that I have written thus far:

import java.util.Scanner;
public class CalculateGrade {

    public static void main(String[] args) {
	//main method calls AssignvalueToArray method, and AssignValueToArray method calls
	//DisplayvalueInArray method
	
	//create a Scanner object for input
	Scanner keyboard = new Scanner(System.in);
	System.out.print("Enter the number of students: ");
	int studentNum = keyboard.nextInt();
	System.out.println();
	System.out.print("Enter the number of Exams: ");
	int studentExamNo = keyboard.nextInt();

	AssignValueToArray(studentNum, studentExamNo);

    }
    
  //This method assigns students' scores to a two dimensional array
    public static void AssignValueToArray(int studentNumber, int numberOfExam)
    {
    int[][] studentScore = new int[studentNumber][numberOfExam];
    Scanner keyboardForArray = new Scanner(System.in);
    
    int studCount = 1;
    for (int index = 0; index < studentNumber; index++)
    {
	System.out.println ("Please type in student " + studCount + " 's grades: " );
	for(int indexOfExam=0; indexOfExam < numberOfExam; indexOfExam++)
    {
	    studentScore[index][indexOfExam] = keyboardForArray.nextInt();
    
	studCount++;
    }
    
    DisplayvalueInArray(studentScore);
    }
    }




    
  //this method displays student score in the two dimensional array
    public static void DisplayvalueInArray(int[][] studentScoreArray)
    {
    System.out.println ("The students' scores are: " + "\n");
    int studentCount = 1;
    for (int index = 0; index < studentScoreArray.length; index++)
    {
    System.out.print("Grades for student " + studentCount +": ");
    for (int indexOfExam = 0; indexOfExam < studentScoreArray[index].length;
    indexOfExam++)
    {
	char letterGrade;
	    
	    if (studentScoreArray[index][indexOfExam] <= 59)
	    {
	    letterGrade = 'F';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 60 && studentScoreArray[index][indexOfExam] <=69)
	    {
	    letterGrade = 'D';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 70 && studentScoreArray[index][indexOfExam] <=79)
	    {
	    letterGrade = 'C';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 80 && studentScoreArray[index][indexOfExam] <= 89)
	    {
	    letterGrade = 'B';
	    }
	    else
	    letterGrade = 'A';
	    
	    
    System.out.print(studentScoreArray[index][indexOfExam]+"\t");
    System.out.print(letterGrade);
    }
    System.out.println();
    studentCount++;
    	}
    
    }
    
}


The purpose of this program is to take the number of students and number of exams from the user, as well as the number grades, and then output the students grade and letter grade when the user has finished inputting the data. The problem I am running into is when I input more than one students grades I get this as the output:

Enter the number of students: 2

Enter the number of Exams: 2
Please type in student 1 's grades:
88
75
The students' scores are:

Grades for student 1: 88 B75 C
Grades for student 2: 0 F0 F
Please type in student 3 's grades:

After I insert student three's grades I get this output:

Please type in student 3 's grades:
66
88
The students' scores are:

Grades for student 1: 88 B75 C
Grades for student 2: 66 D88 B

So, it skips the third students grades, but outputs the grades as student two's. I am not sure if this bug is being caused within the array or when the program gets the letter grade from the inputted number grade. Any help you guys can give me on this problem would be much appreciated and thank you all in advance.

mattyg1192003
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

I don't see (off hand) your problem, but I do have a suggestion, if you turn your if statement around, then you only have to check one condition each time (rather than two), i.e.

if (whatever >= 90) {
    // A
} else if (whatever >= 80) {
    // B
...
} else {
    // F
}


Edit: I would alsostrongly suggest passing the already created Scanner object to the method rather than wrapping System.in with yet another one.

Edit Again: I would, however, say to move the "studCount++" outside of the inner for loop, though.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Salmo Aliko.

your logic seems good but you have two problems
the first you increment studCount in the inner loop you should call it at the outer loop , Ok?

the second you call DisplayvalueInArray(studentScore); while you still take input from the user which in java initialize the rest of the array with zeros , here ur code after i fix it :D

import java.util.Scanner;
public class CalculateGrade {

    public static void main(String[] args) {
	//main method calls AssignvalueToArray method, and AssignValueToArray method calls
	//DisplayvalueInArray method
	
	//create a Scanner object for input
	Scanner keyboard = new Scanner(System.in);
	System.out.print("Enter the number of students: ");
	int studentNum = keyboard.nextInt();
	System.out.println();
	System.out.print("Enter the number of Exams: ");
	int studentExamNo = keyboard.nextInt();

	AssignValueToArray(studentNum, studentExamNo);

    }
    
  //This method assigns students' scores to a two dimensional array
    public static void AssignValueToArray(int studentNumber, int numberOfExam)
    {
    int[][] studentScore = new int[studentNumber][numberOfExam];
    Scanner keyboardForArray = new Scanner(System.in);
    
    int studCount = 1;
    for (int index = 0; index < studentNumber; index++)
    {
	System.out.println ("Please type in student " + studCount + " 's grades: " );
	for(int indexOfExam=0; indexOfExam < numberOfExam; indexOfExam++)
    {
	    studentScore[index][indexOfExam] = keyboardForArray.nextInt();
    
	
    }
    studCount++;
    }
        DisplayvalueInArray(studentScore);

    }




    
  //this method displays student score in the two dimensional array
    public static void DisplayvalueInArray(int[][] studentScoreArray)
    {
    System.out.println ("The students' scores are: " + "\n");
    int studentCount = 1;
    for (int index = 0; index < studentScoreArray.length; index++)
    {
    System.out.print("Grades for student " + studentCount +": ");
    for (int indexOfExam = 0; indexOfExam < studentScoreArray[index].length;
    indexOfExam++)
    {
	char letterGrade;
	    
	    if (studentScoreArray[index][indexOfExam] <= 59)
	    {
	    letterGrade = 'F';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 60 && studentScoreArray[index][indexOfExam] <=69)
	    {
	    letterGrade = 'D';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 70 && studentScoreArray[index][indexOfExam] <=79)
	    {
	    letterGrade = 'C';
	    }
	    else if (studentScoreArray[index][indexOfExam] >= 80 && studentScoreArray[index][indexOfExam] <= 89)
	    {
	    letterGrade = 'B';
	    }
	    else
	    letterGrade = 'A';
	    
	    
    System.out.print(studentScoreArray[index][indexOfExam]+"\t");
    System.out.print(letterGrade);
    }
    System.out.println();
    studentCount++;
    	}
    
    }
    
}
AhmedGhazey
Junior Poster in Training
50 posts since Aug 2010
Reputation Points: 2
Solved Threads: 1
 

happy assignment.

AhmedGhazey
Junior Poster in Training
50 posts since Aug 2010
Reputation Points: 2
Solved Threads: 1
 

Thank you so much Ahmed! I can't believe I didn't spot that before, but like they say it is always the simplest problems that throw you for a loop! Thanks again everyone for all of the help and I greatly appreciate it! :)

mattyg1192003
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

URW any times PLZ Mark it as Solved Thread .

AhmedGhazey
Junior Poster in Training
50 posts since Aug 2010
Reputation Points: 2
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: