Results 1 to 9 of 9
  1. #1
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default need help to fix my errors

    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.
    total no of grades
    Number of A's
    Number of B
    Numbers of C
    Number of D
    number of F

    Java Code:
    import java.util.Scanner;
    public class ExamScores1
    {
    	public static void main (String[]args)
    	{
    		String a;
    		int count= 0;
    
    		int grade=0;     // you declare grade = 0 here
    		int A=0;
    		int B=0;
    		int C=0;
    		int D=0;
    		int F=0;
    
    
    
    
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Enter your exam grades");
    
    
    		a=scan.nextLine();
    		int len=a.length ();
    
    		for (int i=0; i< len; i++)
    		{
    			if (a.charAt (i) ==' '){
    				count--;
    			}else {
    				count++;
    
    			}
    		}
    
    
    // grade is still == 0 here
    
    		if (grade > 90 && grade <= 100) {
    			grade= 'A';
    			A++;
    			count++;
    		} else if (grade > 80 && grade <= 89) {
    			grade= 'B';
    			B++;
    			count++;
    
    		} else if (grade > 70 && grade <= 79) {
    			grade= 'C';
    			C++;
    			count++;
    
    		} else if (grade > 60 && grade <= 69) {
    			grade= 'D';
    			D++;
    			count++;
    		} else if (grade> 0 && grade <= 59) {
    			grade= 'F';
    			F++;
    			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);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help to fix my errors

    You're not doing your large if/else block inside of the for loop and so it's being called only once, and grade isn't even set when it's called. I would:

    Put that code inside of the loop, and get the numeric value of each grade in the for loop. You will probably need to use a while loop not a for loop and use your Scanner to scan through the entered String and to pull out the grade ints. By the way, what is the form of their input? Should you tell the user how exactly they should enter the grades?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: need help to fix my errors

    Java Code:
    if (grade > 90 && grade <= 100) {
                 grade= 'A';
    Let's take a look at your code. Imagine that grade was 95. That makes the if statement true and enters that branch. The very next thing it does is set grade the the char 'A'. What is the point of that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: need help to fix my errors

    Scanner is a class in the Java API. If you are unfamiliar with it I suggest looking it up in the API. In short it a class that is used to gather user input.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help to fix my errors

    Quote Originally Posted by KimChi93 View Post
    @anyone that can answer.
    Please don't hijack someone else's thread with your question. Instead ask your question in its own thread.

  6. #6
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: need help to fix my errors

    thats what i m trying to do.i want numerical grade.
    Quote Originally Posted by Junky View Post
    Java Code:
    if (grade > 90 && grade <= 100) {
                 grade= 'A';
    Let's take a look at your code. Imagine that grade was 95. That makes the if statement true and enters that branch. The very next thing it does is set grade the the char 'A'. What is the point of that?

  7. #7
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: need help to fix my errors

    can u plz explain me how to do that. i would tell user to enter his grades.so when user will enter numeric grades. i will get no of a,b,c,d,f he got.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: need help to fix my errors

    Quote Originally Posted by ali1 View Post
    thats what i m trying to do.i want numerical grade.
    So why are you assinging it a char? It makes no sense.

  9. #9
    ali1 is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 06:52 PM
  3. Errors,errors and errors
    By xpatiencex in forum New To Java
    Replies: 6
    Last Post: 04-25-2009, 04:43 AM
  4. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 08:05 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •