Hi, I'm a student of Java, and my first homework assignment is more advanced than our reading. So...I'm lost.
This question is:

Q5: Write a program that reads a list of exam scores from the keyboard. Using A=90 to 100, B=80 to 89, C=70 to 79, D=60 to 69 and F=0-59 compute: (i)The total number of students, (ii) the numbers for various grades, (iii) the percentage of total number of students for each letter grade, (iv) the range of scores for each letter grade and (v) the average score for each grade as well as for (vi) the entire class. Use a sentinel value of -1 to stop input of marks, which is not to be used in your calculations.[20]

I think I could do this with one number or an array that I could determine, but I can't figure out how to do this with an unknown array provided by the user. I KNOW my code thus far is wrong, and that's why I'd like some pointers. (It's currently a hodgepodge of things I saw that I thought might apply.)

``````import java.util.*;
import java.util.Scanner;

public class Grades /* This program breaks numerical grades into letter grades, computes the number of students,
the number of students per letter grade, the range of scores for each letter grade, the
average score per grade and the class average. */
{
public static void main(String[] Args)
{
System.out.println("Enter exam scores, followed by -1 when you are done entering scores.");
Scanner keyboard = new Scanner(System.in);
System.out.println ("You entered the grades for " + allGrades.length + " students.")
int [] allGrades = new int;
boolean numbersLeft = true
while (numbersLeft)
{
next = keyboard.nextInt( );
if (next = < 0)
numbersLeft = false;
for int i = 0; i < allGrades.length; i++; {
}
}``````

I know I've barely scratched the surface, but it's already off and I'd like to get on track before I tackle the other parts of the question, declare letterGrade, etc. Any pointers would be greatly appreciated. Thanks!

Edited by Ezzaral: Added code tags. Please use them to format all code that you post.

4
Contributors
4
Replies
9
Views
6 Years
Discussion Span
Last Post by Er. Mukesh
``````int []allGrades = keyboard.nextLine( );
System.out.println ("You entered the grades for " + allGrades.length + " students.")
int [] allGrades = new int;``````

not sure what you're trying to do here, but it looks to me this 'll blow up in your face.
you're declaring an array with an identical name twice, which is bound to give you some errors, but also you're instantiating it wrong.

keyboard.nextLine() will return a String object, not an array of integers, and new int .. well, it's not a correct declaration for an array.

I think what you're looking for, is more like this:

``````...
System.out.println("How many grades do you want to enter?");
int num = Integer.parseInt(keyboard.nextLine());
// here you read the value and transfer it to an int, and pass this number on
// while creating the array. this number will
// tell the compiler the size of the array you're declaring
for ( int a = 0; a < num; a++ ){
System.out.println("Enter the grade for student " + (a+1));
}
..``````

This should do it. Explanation is in the code comments. I'll leave the averages to you as an exercise ok?
Hint the easiest way would be to add another array. As you maintain a count of grades, maintain a count of grade totals and divide.
Before dividing, sum the totals and divide by the number of students to get the total average.

``````import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

public class help {

public static void main(String[] args) throws IOException{
System.out.println("Enter exam scores, followed by -1 when you are done entering scores.");
String userInput = br.readLine(); //reads the next line of the buffer upto the newline character (\n "the enter key")
ArrayList<Integer> allGrades = new ArrayList<Integer>(); //a datastructure which works very much like an array except it can change its length on the fly. A useful thing to remember is that the underlying implementation actually relies on an array, but the data structure doesn't make you think about how ;)
Scanner scan = new Scanner(userInput).useDelimiter(",");//assumption: the grades are commaseparated. Separates the string along the delimiters into smaller strings
while(scan.hasNext()){
try{ //tries to do this
int grade = Integer.parseInt(scan.next().trim()); //takes the next scanner elements, sheds all leading and trailing whitespace (trim()) and tries to convert to integer. The next if conditions check whether the integer is valid in the context of -1<x<100
break;
} else if(grade < 0) {
System.out.println("You did not enter a valid grade. Grades cannot be less than 0");
System.out.println("You did not enter a valid grade. Grades cannot be greater than 100");
} else{
allGrades.add(grade); //if the input is valid, insert it at the end of the ArrayList, and increase the ArrayList's size by one
}
} catch (NumberFormatException x){ //if the string could not be converted, for instance if somebody typed in letters, try fails and catch executes instead
System.out.println("You did not enter a valid number as a grade. Sorry"); //prints error message
System.exit(1); //and shuts down the program
}
}
if(allGrades.size()!=0){ //the arraylist version of if(array.length !=0)
System.out.println("There are "+ allGrades.size() + "students");
int[] marks = new int[5]; // a new array to store the five possible marks
Arrays.fill(marks, 0); //initializes every member of the array to 0. I forget if this is or is not done by default. Better safe than sorry ;)
for (int x = 0; x < allGrades.size(); x++){//iterate over every element in the ArrayList
int mark = allGrades.get(x); //equivalent to an array's array[x]
if(mark < 60){ //counts how many people have each of these marks
marks[0]++;
} else if (mark < 70){
marks[1]++;
} else if(mark < 80){
marks[2]++;
} else if(mark < 90){
marks[3]++;
} else {
marks[4]++;
}
}
//the reason I multiply everything by 1.0 is that Java is statically typed. What that means is that 5/2=2 Because it takes integer 5/2 = 2.5, and truncates the.5 to get an integer. By multiplying it by 1.0, I get it to keep everything beyond the floating point. This is also the reason why i have the allGrades.size()!=0 check far above, to avoid a divide by zero error.
System.out.println("There are a total of "+ marks[0] +"'F' grades, which accounts for " + (marks[0]*1.0/allGrades.size())*100 + "% of the student body");
System.out.println("There are a total of "+ marks[1] +"'D' grades, which accounts for " + (marks[1]*1.0/allGrades.size())*100 + "% of the student body");
System.out.println("There are a total of "+ marks[2] +"'C' grades, which accounts for " + (marks[2]*1.0/allGrades.size())*100 + "% of the student body");
System.out.println("There are a total of "+ marks[3] +"'B' grades, which accounts for " + (marks[3]*1.0/allGrades.size())*100 + "% of the student body");
System.out.println("There are a total of "+ marks[4] +"'A' grades, which accounts for " + (marks[4]*1.0/allGrades.size())*100 + "% of the student body");

} else {
System.out.println("Sorry, you did not enter any valid grades");
}

}
}``````

BufferedReader and InputStreamReader. Without getting into too much detail, their good practice, use them to read input.

Edited by apolishch: n/a

``int []allGrades = keyboard.nextLine( );``

you're trying to set an array of integers to a string. Java is statically typed (as discussed in my code above). You can NOT even set int = 5.0. This WILL crash. And you're trying to set completely unrelated objects ;)

``int[] allGrades =new int;``

not only are you assigning an array to a number (impossible), you are instantiating (creating) an array that already exists. Also problem.
If you were going to reset it you'd just drop the type declaration and it wouldbecome

``allGrades = new int[someLength];``

also:

``next = keyboard.next();``

This is a problem because the compiler does not know what type next is. You would have to say:

``String next = keyboard.next();``

and just a suggestion:

Don't do this:

``````boolean someBoolean = true;
while(someBoolean){
foo();
if(exitCondition==true){
someBoolean = false;
}
}``````

``````while(true){
foo();
if(exitCondition){
break;
}
}``````

Edited by apolishch: n/a

I have done J2EE Degree certifications and following just call me..
I have 6 years of experience in I.T. industry and now running my own software company Mnc Software Pvt. Ltd. We are also providing training in Java,J2EE,PHP etc. So i am looking for those interested students. If they successfully completed their training then we offer job. Contact: 0141-2251755

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.