-2

I'm really beginner trying to learn code and based on the literature I read I created the following program. The problem with this code is that any number I enter returns GRADE F. If I remove System.in.read() and set permanent value then it works just fine. Anyone guessing what could be wrong with my code?

class ladder2 {    
  public static void main(String args[])  
    throws java.io.IOException {

    char read, grade;
    // read = 75;   

    System.out.println("Enter a score between 0 and 100: ");
    read = (char) System.in.read();

    if (100 <= read) grade = 'A'; 
    else if (85 <= read) grade = 'B'; 
    else if (75 <= read) grade = 'C';
    else if (60 <= read) grade = 'D';
    else grade = 'F';

    System.out.println("GRADE " + grade);   
  }
}
Pikola Pika
  • 11
  • 1
  • 3
  • Use switch case java. – Makky Nov 02 '13 at 08:01
  • Asker is learning and just misused `read()` method. Why to silently downvote? – Vadzim Nov 02 '13 at 08:03
  • `Scanner sc = new Scanner(System.in); read = sc.nextInt();` – Serge Seredenko Nov 02 '13 at 08:03
  • @Makky Could you show please how exactly you are suggesting to use switch? I can't figure out. – Serge Seredenko Nov 02 '13 at 08:05
  • 2
    The *first* `if..else if..` which has a true condition is the *only* branch that will run. So, even when `read` is fixed, the output will be wrong. – user2864740 Nov 02 '13 at 08:09
  • I wouldn't worry about not using `switch` statement when you're still learning. Just make sure you do learn it at some point, but just using `if..else if..else` stuff is perfectly fine, some might say even preferable if you're not using an enum (which you probably have not learned yet either :-). – hyde Nov 02 '13 at 08:15
  • Another hint for learning: generally use the `{}` with `if`, `while` etc, even when you have just one statement. It's too easy to make a mistake if you don't. Though *this* code might be an acceptable exception, since this is so simple and every "then" part has assignment to same variable. But if you do anything more varied, use `{}` and multiple lines, even if you have just one statement in there. It will save you a ton of debugging stupid mistakes. – hyde Nov 02 '13 at 08:21

3 Answers3

2

Use BufferedReader, also you grade logic is not correct

import java.io.BufferedReader;
import java.io.InputStreamReader;

class del {    
   public static void main(String args[]) throws java.io.IOException {

int read;char  grade;
 read = 75;   

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a score between 0 and 100: ");
read = Integer.parseInt(br.readLine());

if (100 >= read && read >85) grade = 'A'; 
else if (85 >= read && read >75) grade = 'B'; 
else if (75 >= read && read >60) grade = 'C';
else if (60 >= read && read >50) grade = 'D'; // you may want to change 50
else grade = 'F';
        System.out.println("GRADE " + grade);   
    }
}
Ankit Rustagi
  • 5,203
  • 10
  • 34
  • 65
  • I must admit that `if` usage was not wrong in the question. But is wrong in this answer. ) – Vadzim Nov 02 '13 at 08:12
  • 1
    @Vadzim It is wrong in the original question. All values <= 100 will get a grade of "A". – user2864740 Nov 02 '13 at 08:13
  • Well, I didn't notice the cap of 100 and assumed legal that only exceptional answers of score >= 100 are awarded with grade A. ) Still there is not need to double check `read` both higher and lower bound. Particularly, comparing it with 85 in each case. It's sufficient to check just lower bound as in question. – Vadzim Nov 02 '13 at 08:16
  • Code is good, but this answer fails to explain, how using `read` to read a `char` is wrong in the original code. – hyde Nov 02 '13 at 08:26
  • I wanted to point OP out in the right direction and leave something for him to figure out. Its nothing he wont find from a simple google search. – Ankit Rustagi Nov 02 '13 at 08:36
  • Hey guys, thanks so much! Works like a charm now. And all the mentioned comments, they were very useful to me. – Pikola Pika Nov 02 '13 at 12:05
1

read() returns charcode of the first input symbol.

But you need to read the whole number symbols to be parsed as int.

Community
  • 1
  • 1
Vadzim
  • 21,258
  • 10
  • 119
  • 142
1

This example uses nested if.

package botball;
import java.util.Scanner;

public class bots {
  public static void main(String[] args){
    int a;
    Scanner Cin = new Scanner (System.in);
    a = Cin.nextInt();

    if (a<=100);{
      if (a>90)
        JOptionPane.showMessageDialog(null,"A1");
      else {
        if (a>80)
          JOptionPane.showMessageDialog(null,"A2");
        else { 
          if (a>70)
            JOptionPane.showMessageDialog(null,"B1");
          else {
            if (a>60)
              JOptionPane.showMessageDialog(null,"B2");
            else {
              if (a>50)
                JOptionPane.showMessageDialog(null,"C1");
              else {
                if (a>40)
                  JOptionPane.showMessageDialog(null,"C2");
                else {
                  if (a>30)
                    JOptionPane.showMessageDialog(null,"D");
                  else {
                    if (a>20)
                      JOptionPane.showMessageDialog(null,"E1");
                    else
                      JOptionPane.showMessageDialog(null,"E2");
                  }
                }
              }
            }
          }
        }
      }
    }
Pardeep Dhingra
  • 3,746
  • 6
  • 27
  • 52
anonymous
  • 11
  • 1