What's wrong?, results not correct

P

p.munsu

/* Character input.
a) use System.in.read() method to enter a single character into a
variable and print contents of this
variable
b) Use System.in.read() method to enter grade character and print the
corresponding mark range as per
the following scheme:
Grade Mark range
A 80-100
B 70-79
C 55-69
D 45-54 */


public class p4no5 {



public static void main(String args[]) throws Exception {

char x;
int i = 0;



/*System.out.print("Please type a character: ");
x = (char) System.in.read(); */


System.out.print("Please Enter Grade: ");
i = System.in.read();




//System.out.println(x);

if( i < 45 ) {
System.out.println("F");

} else if( i >= 45 && i <= 54 ) {
System.out.println("D");
} else if( i >= 55 && i <= 69 ) {
System.out.println("C");
} else if( i >= 70 && i <= 79 ) {
System.out.println("B");
} else if( i >= 80 && i <= 100 ) {
System.out.println("A");
}


}
}
 
P

Patricia Shanahan

/* Character input.
a) use System.in.read() method to enter a single character into a
variable and print contents of this
variable

Have you done part (a)? If not, I strongly suggest doing it first.

b) Use System.in.read() method to enter grade character and print the
corresponding mark range as per
the following scheme:
Grade Mark range
A 80-100
B 70-79
C 55-69
D 45-54 */

Your code seems to assume that the input will be something to do with
the "Mark range", but the instructions say "enter grade character".

You also need to (re)read the API documentation for read in class
InputStream.

Patricia

public class p4no5 {



public static void main(String args[]) throws Exception {

char x;
int i = 0;



/*System.out.print("Please type a character: ");
x = (char) System.in.read(); */


System.out.print("Please Enter Grade: ");
i = System.in.read();




//System.out.println(x);

if( i < 45 ) {
System.out.println("F");

} else if( i >= 45 && i <= 54 ) {
System.out.println("D");
} else if( i >= 55 && i <= 69 ) {
System.out.println("C");
} else if( i >= 70 && i <= 79 ) {
System.out.println("B");
} else if( i >= 80 && i <= 100 ) {
System.out.println("A");
}


}
}
 
F

frankgerlach22

You have to use an InputStreamReader in order to
read characters from System.in. Your code is reading
bytes from the console, not characters.
The following does what you want:

public class p4no5 {

public static void main(String args[]) throws Exception {

char x;
int i = 0;

System.out.print("Please Enter Grade: ");

InputStreamReader isr=new InputStreamReader(System.in);
char[] ca=new char[2];
isr.read(ca,0,2);
String s=new String(ca);

i=Integer.parseInt(s);



if( i < 45 ) {
System.out.println("F");

} else if( i >= 45 && i <= 54 ) {
System.out.println("D");
} else if( i >= 55 && i <= 69 ) {
System.out.println("C");
} else if( i >= 70 && i <= 79 ) {
System.out.println("B");
} else if( i >= 80 && i <= 100 ) {
System.out.println("A");
}

}
}
 
F

frankgerlach22

Oh, BTW, a case statement would be more elegant than the if
-else-if-else statement...
 
P

Patricia Shanahan

You have to use an InputStreamReader in order to
read characters from System.in. Your code is reading
bytes from the console, not characters.
The following does what you want:

This appears to be a student exercise, with a specific implementation
requirement, "Use System.in.read() method". I assume one of the
objectives is for the OP to learn the exact meaning of System.in.read()
by using it in a simple program.

Like the OP, you treated the input as a numeric mark and calculated a
grade character, despite the instructions which say "enter grade
character and print the corresponding mark range".

In any case, I don't think providing code solutions to student exercises
does the student any service. The program itself is not useful. The
benefit to the student lies in the act of writing it, and getting it to
work. Providing code, rather than advice, deprives them of that benefit.

Patricia
 
J

John B. Matthews

/* Character input. [...]
b) Use System.in.read() method to enter grade character and print the
corresponding mark range as per
the following scheme:
Grade Mark range
A 80-100
B 70-79
C 55-69
D 45-54 */
[...]

I would interpret this to mean the following: input a "grade character"
and output a "mark range", e.g.

...
System.out.print("Please Enter Grade: ");
i = System.in.read();

if (i == 65 | i == 97) { // the letters "A" or "a"
System.out.println("80 - 100");
}
...
 
G

Gordon Beaton

if (i == 65 | i == 97) { // the letters "A" or "a"

Please - do yourself and anyone else who reads your code a favour and
specify letters by their real names, instead of converting them to
numeric values that may or may not be correct:

if (i == 'A' || i == 'a') ...

/gordon
 
J

John B. Matthews

Gordon Beaton <[email protected]> said:
Please - do yourself and anyone else who reads your code a favour and
specify letters by their real names, instead of converting them to
numeric values that may or may not be correct:

if (i == 'A' || i == 'a') ...

/gordon

'A' == 65 and 'a' == 97 are correct.

While 'A' and 'a' are indeed more readable, the OP was apparently having
trouble interpreting the int returned by read(). I chose to make the
result explicit, using the comment to highlight the equivalence.

It's never to early to learn good style, but the poor student has to
start _somewhere_! :)
 
P

Patricia Shanahan

John said:
'A' == 65 and 'a' == 97 are correct.

While 'A' and 'a' are indeed more readable, the OP was apparently having
trouble interpreting the int returned by read(). I chose to make the
result explicit, using the comment to highlight the equivalence.

It's never to early to learn good style, but the poor student has to
start _somewhere_! :)

I'm very troubled by the bad example posters are setting for the OP. The
OP started out by apparently not reading the problem specification very
carefully. That is to be expected, because this looks like a beginner
exercise, and it takes time to learn how very important it is to read
specifications carefully.

However, people answering the question, who should know better and be
setting a good example, are also ignoring the specification.

Where does 'a' or 97 come from? The grade character for the 80-100 mark
range is 'A'.

The problem specification does not list lower case grade characters. It
does not say that lower case equivalents can be accepted. It does not
even use the term "grade letter" for which one could argue that 'A' and
'a' are the same letter. It says 'grade character' and 'a' is not the
same character as 'A'.

Accepting 'a' both deviates from the specification, and makes the
program more difficult than necessary, so why do it?

Patricia
 
J

John B. Matthews

Patricia Shanahan said:
I'm very troubled by the bad example posters are setting for the OP.

Can you be more specific? To which bad example do you refer?
The OP started out by apparently not reading the problem
specification very carefully. That is to be expected, because this
looks like a beginner exercise, and it takes time to learn how very
important it is to read specifications carefully.

However, people answering the question, who should know better and be
setting a good example, are also ignoring the specification.

Where does 'a' or 97 come from? The grade character for the 80-100 mark
range is 'A'.

The problem specification does not list lower case grade characters. It
does not say that lower case equivalents can be accepted. It does not
even use the term "grade letter" for which one could argue that 'A' and
'a' are the same letter. It says 'grade character' and 'a' is not the
same character as 'A'.

Accepting 'a' both deviates from the specification, and makes the
program more difficult than necessary, so why do it?

Patricia

Indeed, accepting 'a' as input is outside of the specification and
potentially confusing. I stand corrected.
 
P

Patricia Shanahan

John said:
Can you be more specific? To which bad example do you refer?

Specification ignoring. I thought I was quite specific in the rest of
the message.

There was another example earlier in this thread which included a
solution that did not use the required System.in.read() method, and
calculated grade character from mark, instead of calculating mark range
from grade character.

Patricia
 
F

frankgerlach22

whatabout
switch(var){
case 1:
case 2:
case 3: doSoemthingforOneToThree();
break;
....
 
T

Tor Iver Wilhelmsen

whatabout
switch(var){
case 1:
case 2:
case 3: doSoemthingforOneToThree();
break;
...

That would work, but only be acceptable for small ranges. There isn't
that much of a performance hit with if/else if and comparisons
compared to using a large switch block.
 
M

Monique Y. Mudama

That would work, but only be acceptable for small ranges. There isn't
that much of a performance hit with if/else if and comparisons
compared to using a large switch block.

Are switches expensive?
 
R

Roedy Green

Are switches expensive?

there are two flavours of switch that each map to a different JVM
instruction.

The fast kind is a jump table where you index by the value. These
work where the range is dense.

The slow kind is like a set of nested ifs where it compares each value
in turn (or perhaps does a binary search). The implementation is
hidden inside the JVM. The compiler uses this when the values are
sparse (many or big holes in the number range).

My thinking is SWITCH should be extended handle ranges, and the
compiler should be much cleverer than now about generating optimal
code.

see http://mindprod.com/projects/caserange.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top