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.

Recommended Answers

All 5 Replies

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 also strongly 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.

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++;
    	}
    
    }
    
}
commented: Don't just correct people's homework and hand it to them. -3
commented: We do not do others' homework here. It [i]does not[/i] help them. -3

happy assignment.

commented: lame way to spike up your 'posts in solved threads' count .. might even have been a better assignment if he had made it himself -2

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! :)

URW any times PLZ Mark it as Solved Thread .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.