Can you spot what is wrong with the java code that I am doing. Here is the java code that I have been working on...

import java.io.*;
public class Konata
{
public static void main
(String args [])
{
BufferedReader br=newInputStreamReader(System.in);
int grade,answer=0;
do
{
System.out.print ("\n Enter grade");
grade=Integer.parseInt(br.readline());
if ((grade>=90)||(<=100))
System.out.print("\n A");
else if ((grade>=80)||(<=89))
System.out.print("\n B");
else if ((grade>=70)||(<=79))
System.out.print("\n C");
else if ((grade>=60)||(<=69))
System.out.print("\n F");
else 
System.out.print("\n out of range");
System.out.print("\n Do you want to try again? Press 1 for Yes and Press 0 for No:");
answer=Integer.parseInt(br.readLine());
}while (answer==1);
}
}

whatever number i type, the only answer read is A? thank you so much for the help!

Recommended Answers

All 6 Replies

the if statements are incorrect; in Java, you cannot compare a range the way you are trying to do. This is a common misunderstanding, as it makes sense mathematically, but doesn't work in most programming languages.

You need to write out a full clause on each side of the or operator:

import java.io.*;
public class Konata {
    public static void main(String args []) {
        BufferedReader br=newInputStreamReader(System.in);
        int grade,answer=0;

        do {
            System.out.print ("\n Enter grade");
            grade=Integer.parseInt(br.readline());

            if ((grade>=90)||(grade<=100)) {
                System.out.print("\n A");
            }
            else if ((grade>=80)||(grade<=89)) {
                System.out.print("\n B");
            }
            else if ((grade>=70)||(grade<=79)) {
                System.out.print("\n C");
            }
            else if ((grade>=0)||(grade<=69)) {
                System.out.print("\n F");
            }
            else { 
                System.out.print("\n out of range");
            }

            System.out.print("\n Do you want to try again? Press 1 for Yes and Press 0 for No:");
            answer=Integer.parseInt(br.readLine());
        }while (answer==1);
    }
}

Note that I reformatted the code according to the Java standard. You should generally follow this standard, as it makes the code more familiar and more readable.

if ((grade>=90)||(grade<=100)) {)

Does this return true if grade is 20?
(note: 20 is < 100 so the second condition is true and the whole OR connected condition is true)
Do you want both conditions to be true or only one of them?
Both requires AND
only one requires OR

Oops. I was so caught up in fixing the syntactic problem that I completely ignored the logical flaws.

Member Avatar for hfx642

Well, for a start... Replace your ORs ("||") with ANDs ("&&").

Member Avatar for ztini

You can drop a lot of the logic if you check for bad conditions first. There is no right answer, but this might be a bit cleaner.

if (grade > 100 || grade < 0)
                System.out.print("\n out of range");
            else if (grade >= 90 )
                System.out.print("\n A");
            else if (grade >= 80)
                System.out.print("\n B");
            else if (grade >= 70)
                System.out.print("\n C");
            else if (grade >= 60)
                System.out.print("\n D");
            else
                System.out.print("\n F");
Member Avatar for ztini

Also, instead of doing:

System.out.print("\n rawr");

You could use:

System.out.println("rawr");

The println does a return line after the "rawr". This is a little different from yours which returns before the line. However, I found that carefully placing printlns eliminates the need for '\n' almost all together.

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.