import java.util.*;
import java.io.*;

public class stundentGrade {

    public static void main(String args [])throws IOException{
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

        int a=0, b=0,c=0,d=0,f=0, totalNumber=0; //declaration of initialization

        stars(); //method to call and print stars
        System.out.println("Enter a list of Exam scores from 0-100" );
        System.out.println("use a negative number if you'r done with your list");
        stars();//method to call and print stars


        for (int grade=0; grade>=0 ;totalNumber++)
        {
            try
            {

        grade=Integer.parseInt(dataIn.readLine());//Handles the Exam score
        if(grade>=90 && grade <=100)
            a++;
        else if(grade >= 80 && <= 89)
            b++;
        else if(grade >= 70 && <= 79)
            c++;
        else if(grade >= 60 && <= 69)
            d++;
        else if(grade >= 0 &&  <= 59)
            f++;
        else if(grade > 100)

        {
            System.out.println("Invalid grade!");//Error message
            totalNumber--;//remove the invalid grade
        }
            }
            catch(NumberFormatException e)
            {


            System.out.println("Invalid Imput! Please enter your score in number format"); // error message
            totalNumber--;//remove the invalid input
            continue;
        }



        }

            System.out.println();
            System.out.println(" Total number of grade: " + (totalNumber-1));
            System.out.println(" A grades: "+ a);
            System.out.println(" B grades: "+ b);
            System.out.println(" C grades: "+ c);
            System.out.println(" D grades: "+ d);
            System.out.println(" F grades: "+ f);
            }   
        public static void stars()
        {
        System.out.println("***************************************");
        }
}

Recommended Answers

All 14 Replies

Why do you think anything is wrong with it? If you have an error, or incorrect results, then share that info with us, don't expect us to guess!

commented: Good question. ;-> +6
commented: yes you are right, but i read that if incase we have problem with code we just need to post´. but seeing the code was enought to analys the problem +0

it doesn't handle negative grades...

commented: Yes; that's what we needed to know. +6

You have to implement what happens if the grade is less than 0. Currently there is nothing and it just won't do anything about it

commented: Yes; exactly! +6

you have to write all else if(grade>=80 && grade<=89), and why are you using for loop just remove it.

commented: Makes no sense. Loop IS needed +0
commented: because is king of evaluation process, which need to have four loops. or do you have an idea on how to get it in shotcut? +0

I guess many eyes on the same code help discovering all of issues in your code.

You need to redesign your logic here. What are different reasons to pick between a for-loop and do-while loop? One distinct reason is that a for-loop should be used with a pre-defined minimum/maximum value of a control variable and do-while should be used with dynamic condition checker. Your problem needs to dynamically check for a certain condition (line 13), so which loop should you use in the program?

Now what is the condition to be checked? Yes, the grade. Now it is easy if you pick the right loop type. You simply add the condition to it in order to get out/stay in the loop. Also, don't forget to define some variables used inside the loop before the loop starts. Ensure that it is valid before entering the loop. Then inside, use if-elseif-else conditions to check for data entered from user again.

My Problem starts at line 22 Till 33,which gives me red flags.
And in eclips there is a feature to be activated before this line will be compatible and can be executed. Which i fogot that feature, but if anybody have an idea then i will be greatfull trying it.
Thanks

If you hover on the red flags you should see a proper error message. Please post the complete exact text of the error messages.

Sorry i havent Add the flags messege,
The operator && is undefined for the augement type boolean,int

I just fixed the problem, i fogort to fixed in The secound grade Text to them (grade >=80 && grade <=89)

grade=Integer.parseInt(dataIn.readLine());//Handles the Exam score
    if(grade>=90 && grade <=100)
    a++;
    else 
        if(grade >=80 && grade <=89)
    b++;
    else 
        if(grade >= 70 && grade <= 79)
    c++;
    else 
        if(grade >= 60 && grade<= 69)
    d++;
    else 
        if(grade >= 0 && grade <= 59)
    f++;
    else 
        if(grade > 100)
    {

proper indentation would make your code a lot more readable. for someone who just started to read this, it might look as if that first if statement has 5 else blocks attached to it.

also, use brackets { }

even thoug when your code is only one line, it's not mandatory, it helps making the scope clear, and, if you ever have to add code in one of those if blocks, the chances of creating regression bugs are smaller.

Not sure but you should be able to do away with lines 16 & 17. Unless it's possible to get a grade higher than 100.

i think you are mistaking with my code because on the line 16 & 17 had nothing in it. is a start of the code or may be i don't understand what exac what you mean. if you can explain me what you mean. thanx

Your indentation from the original post is correct. The recent one, it is weird and improper...

// I am guessing your intention to the code is as follows:
grade=Integer.parseInt(dataIn.readLine());//Handles the Exam score
if(grade>=90 && grade <=100)
  a++;
else 
  if(grade >=80 && grade <=89)
    b++;
  else 
    if(grade >= 70 && grade <= 79)
      c++;
    else 
      if(grade >= 60 && grade<= 69)
        d++;
      else 
       if(grade >= 0 && grade <= 59)
         f++;
       else 
        if(grade > 100)
  ...

// However, this actually be accomplished with
// if, else if, else statement
// (with proper indentation for readability
//  if not use curry brackets!).
if(grade>=90 && grade <=100)
  a++;
else if(grade >=80 && grade <=89)
  b++;
else if(grade >= 70 && grade <= 79)
  c++;
else if(grade >= 60 && grade<= 69)
  d++;
else if(grade >= 0 && grade <= 59)
  f++;
else if(grade > 100)  // why?
  ...   // if you want to deal with grade>100,
        // how would you deal with grade<0?

The question from henryz_box would be "Is it possible to have a grade higher than 100 because you attempt to handle it in the code line 16-17?" To me it is irrelevant. However, to answer that, it would be nice to handle any out-of-range value if you can because the range of variable you are dealing with is finite (less than 0 - invalid, between 0 and 100 - valid, and greater than 100 - invalid).

And by the way Wandaga1, what does "anything else" mean to you? What should it be replaced with in if-else statement?

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.