• Java
Hi i am looking for following output.so far I am only getting result for total no of grades.I am getting 0 for A B C D and F.
Number of A's
Number of B
Numbers of C
Number of D
number of F
import java.util.Scanner;
public class ExamScores
{
public static void main (String[]args)
{
String a;
int count= 0;

int A=0;
int B=0;
int C=0;
int D=0;
int F=0;

Scanner scan = new Scanner (System.in);

a=scan.nextLine();
int len=a.length ();

for (int i=0; i< len; i++)
{
if (a.charAt (i) ==' '){
count--;
}else {
count++;

}
}

A=A+1;
count++;
B=B+1;
count++;

C=C+1;
count++;

D=D+1;
count++;
F=F+1;
count++;
}
System.out.println("total no of grades=" + count);
System.out.println("Number of A=" + A);
System.out.println("Number of B=" + B);
System.out.println("Number of C=" + C);
System.out.println("Number of D=" + D);
System.out.println("Number of F=" + F);
}
}

Mark44
Mentor
There are several problems with your code.
1. grade is declared as an int and is initialized to 0, but in your if statements, you are attempting to store a character value in it.
2. grade should be given a value from the input string, but your code doesn't do this. None of the conditions in your if statements evaluate to true, so none of the code in the if statements actually executes.
3. Your for loop counts the nonspace characters in the input string. This is not the same as the number of grades in the string.
4. You never get the grades out of the input string. This should happen inside a loop, which should also contain your logic for determining whether a given grade is an A, B, and so on. The Scanner class has some methods that will be helpful; namely, hasNextInt and nextInt. See http://download.oracle.com/javase/7/docs/api/.

i make some changings
Code:
import java.util.Scanner;
public class ExamScores
{
public static void main (String[]args)
{
//create scanner object
Scanner scan = new Scanner(System.in);
System.out.println("How many grades do you have to enter?: ");
//reads in the number of grades user wants to enter as double from keyboard
//now since program knows how many grades are being entered it can
//control the input
for(int j = 1; j <= numberOfGrades; j++)
//based off of number of grades the user inputs it will ask
//reads in exam scores as a double from keyboard
//compares the datafield grade with scores 90-100
//if letter grade is A then A which is an int that is initialized at 0
A = A + 1;
}

//if letter grade is A then A which is an int that is initialized at 0
B = B + 1;
}
//if letter grade is A then A which is an int that is initialized at 0
C = C + 1;
}
//if letter grade is A then A which is an int that is initialized at 0
D = D + 1;
}
//if letter grade is A then A which is an int that is initialized at 0
F = F + 1;
}

System.out.println("Number of A" + A);
System.out.println("Number of B" +B);
System.out.println("Number of C" + C);
System.out.println("Number of D" + D);
System.out.println ("Number of F" + F);

}

}
}

Last edited by a moderator:
Mark44
Mentor
Does it work?

Mark44
Mentor
1. Your code won't compile - there are variables used that aren't declared.
2. The for loop won't work as you expect, as you are missing braces ({ }) around the body of the loop. The structure of the for loop should be like this.
Code:
for(int j = 1; j <= numberOfGrades; j++)
{
// Body of loop
}
3. When you ask the user how many grades will be entered, you shouldn't be using a double to store that value. The user will not be entering 7.36 grades. Use an int.
4. If the grades themselves are whole numbers, use ints to store them.
Code:
if (grade >= 90 && grade <= 100) {
//if letter grade is A then A which is an int that is initialized at 0
A = A + 1;
}
All of the branches have exactly the same comments. The comments should be updated for each of the five sections.
There is no need for a letterGrade variable. You can trim your code down considerably to this:
Code:
if (grade >= 90 && grade <= 100)
{
A_count = A_count + 1;
}

Why are you taking A=A+1, B=B+1, C=C+1, D=D+1, E=E+1, F=F+1 in If, else-if blocks?
Code:
if (grade > 90 && grade <= 100) {
[COLOR="Red"]A=A+1;[/COLOR]
count++;
[COLOR="Red"]B=B+1;[/COLOR]
count++;

[COLOR="Red"]C=C+1;[/COLOR]
count++;

[COLOR="Red"]D=D+1;[/COLOR]
count++;
[COLOR="Red"]F=F+1;[/COLOR]
count++;
}

AlephZero