469,353 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Nested if() statements...Grade range...

tiktik
14
Hi...

I am doing this simple Java program which displays a particular grade (A, B, C...) according to the mark entered.

However I cannot arrange it in such a way that it displays "Invalid" if the user eners a grade > 100... take a look...


Expand|Select|Wrap|Line Numbers
  1. System.out.print ("Enter mark: ");
  2.     int mark = Keyboard.readInt();
  3.     char grade =' ';
  4.  
  5.  
  6.     if (mark > 100) (System.out.println ("Invalid Entry"));
  7.       else if (mark >=80 && mark <= 100) grade = 'A';
  8.          else if (mark >= 60) grade = 'B';
  9.               else if (mark >= 55) grade = 'C';
  10.                      else if (mark >= 40) grade = 'D'; 
  11.                             else if (mark < 40) grade = 'F';
  12.  
  13.  
  14.         System.out.println ("Grade = " + grade);
after compiled it keeps telling me that this is not a statement:

if (mark > 100) (System.out.println ("Invalid Entry"));



can't figure why ..


The program however runs, and if the user enters "101" for exmple, it keeps obeying the second 'else if'....

Any ideas pls....?

thanks
Nov 26 '08 #1
9 7803
Nepomuk
3,112 Expert 2GB
@tiktik
It's right, that isn't a statement. Try
Expand|Select|Wrap|Line Numbers
  1. if (mark > 100) System.out.println ("Invalid Entry");
or
Expand|Select|Wrap|Line Numbers
  1. if (mark > 100) {System.out.println ("Invalid Entry");}
instead.

Greetings,
Nepomuk
Nov 26 '08 #2
tiktik
14
Yes that's it thanks a lot...

but now i have another problem...

i would like that if the entry is invalid, it would not display the last line ("Grade =" ), and so i tried to insert the 'break;' but it seems as if it can be used only in loops or switch :(



Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.  
  3.     System.out.print ("Enter mark: ");
  4.     int mark = Keyboard.readInt();
  5.     char grade =' ';
  6.  
  7.  
  8.     if (mark > 100) System.out.println ("Invalid Entry"); 
  9.         break;
  10.        if (mark >=80 && mark <= 100) grade = 'A';
  11.          else if (mark >= 60) grade = 'B';
  12.               else if (mark >= 55) grade = 'C';
  13.                      else if (mark >= 40) grade = 'D'; 
  14.                             else if (mark < 40) grade = 'F';
  15.  
  16.  
  17.         System.out.println ("Grade = " + grade);

when I run this program and input an invalid entry, it still displays "Grade = " at the end...
Nov 27 '08 #3
tiktik
14
Never mind.... i solved the problem by inserting "System.out.println" after every if statement like this

Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.  
  3.     System.out.print ("Enter mark: ");
  4.     int mark = Keyboard.readInt();
  5.     char grade =' ';
  6.  
  7.  
  8.     if (mark > 100) System.out.println ("Invalid Entry"); 
  9.        else if (mark >=80 && mark <= 100) {grade = 'A';
  10.                                            System.out.println ("Grade = " + grade); }
  11.  
  12.           else if (mark >= 60) {grade = 'B';
  13.                                 System.out.println ("Grade = " + grade); }
  14.  
  15.               else if (mark >= 55) {grade = 'C';
  16.                                        System.out.println ("Grade = " + grade); }
  17.  
  18.                      else if (mark >= 40) {grade = 'D'; 
  19.                                               System.out.println ("Grade = " + grade); }
  20.  
  21.                             else if (mark < 40) {grade = 'F';
  22.                                                  System.out.println ("Grade = " + grade);}

Now the program works as it is supposed... but still I have the question question whether or not the 'break;' could be used elsewhere (not just in loops or switch() )?... It would be very useful I think - especially when one needs to terminate the program if a certain value is entered - one needs something which would get him out of the main() block...

Thanks
Nov 27 '08 #4
JosAH
11,448 Expert 8TB
This is just programming: if a valid numbers was entered you want to do something, otherwise you don't want to do anything, so:

Expand|Select|Wrap|Line Numbers
  1. if (valid number entered) {
  2.    do something;
  3. }
  4.  
Let's concentrate on the 'valid number entered'; the boolean expression
'number <= 100 && number >= 0' seems to express what you want. Simply plug it into your pseudo code:

Expand|Select|Wrap|Line Numbers
  1. if (number <= 100 && number >= 0) {
  2.    do something;
  3. }
  4.  
You already have solved the 'do something' part so plug it into the pseudo code.

kind regards,

Jos
Nov 27 '08 #5
tiktik
14
Yes, thanks for your reply...it works very well with it as well

Any idea about the break; question?
Nov 27 '08 #6
JosAH
11,448 Expert 8TB
@tiktik
As you have already noticed a 'break' statement can only occur in a loop or in a case statement.

kind regards,

Jos
Nov 27 '08 #7
Nepomuk
3,112 Expert 2GB
@tiktik
There's always System.exit(int Status) if you really want to leave your program, but that should be used with caution.

Other than that, you can always check if your grade variable has been assigned yet - setting it to a default value (e.g. 'X') would be one way of doing that. Or you can have a boolean value gradeWasSet. Or you can put all of those things in one nested if. Or I'm sure you can think of something else.

There is something similar to what you mean in many languages: the jump command goto. This command doesn't exist in Java (although there is continue, which is similar in some aspects), as it can make code very ugly very easily. That's also why it's seen as bad style by a great part of the programming world.

Here, I'll even give you a comic about goto:


(from http://xkcd.com/292/)

Greetings,
Nepomuk
Nov 27 '08 #8
tiktik
14
Thanks for the detailed explanation.
good comic as well =)

tiktik
Nov 28 '08 #9
Nepomuk
3,112 Expert 2GB
You're welcome of course. :-)

Greetings,
Nepomuk
Nov 28 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Don Low | last post: by
15 posts views Thread by Xah Lee | last post: by
2 posts views Thread by SplaTTer | last post: by
3 posts views Thread by Tcs | last post: by
10 posts views Thread by nimmi_srivastav | last post: by
3 posts views Thread by Jake Emerson | last post: by
5 posts views Thread by Jyotirmoy Bhattacharya | last post: by
16 posts views Thread by koutoo | last post: by
reply views Thread by Maric Michaud | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.