What's wrong?, results not correct

Discussion in 'Java' started by p.munsu@gmail.com, Aug 7, 2005.

  1. Guest

    /* 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");
    }


    }
    }
    , Aug 7, 2005
    #1
    1. Advertising

  2. wrote:
    > /* 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");
    > }
    >
    >
    > }
    > }
    >
    Patricia Shanahan, Aug 7, 2005
    #2
    1. Advertising

  3. Guest

    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");
    }

    }
    }
    , Aug 7, 2005
    #3
  4. Guest

    Oh, BTW, a case statement would be more elegant than the if
    -else-if-else statement...
    , Aug 7, 2005
    #4
  5. wrote:
    > 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
    Patricia Shanahan, Aug 7, 2005
    #5
  6. In article <>,
    wrote:

    > /* 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");
    }
    ...

    --
    John
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
    John B. Matthews, Aug 7, 2005
    #6
  7. On Sun, 07 Aug 2005 16:30:37 GMT, John B. Matthews wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Aug 8, 2005
    #7
  8. In article <42f713f2$>, Gordon Beaton <>
    wrote:

    > On Sun, 07 Aug 2005 16:30:37 GMT, John B. Matthews wrote:
    > > 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


    '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_! :)

    --
    John
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
    John B. Matthews, Aug 8, 2005
    #8
  9. John B. Matthews wrote:
    > In article <42f713f2$>, Gordon Beaton <>
    > wrote:
    >
    >
    >>On Sun, 07 Aug 2005 16:30:37 GMT, John B. Matthews wrote:
    >>
    >>> 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

    >
    >
    > '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
    Patricia Shanahan, Aug 8, 2005
    #9
  10. In article <D5IJe.2988$>,
    Patricia Shanahan <> wrote:

    > John B. Matthews wrote:
    > > In article <42f713f2$>, Gordon Beaton <>
    > > wrote:
    > >
    > >
    > >>On Sun, 07 Aug 2005 16:30:37 GMT, John B. Matthews wrote:
    > >>
    > >>> 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

    > >
    > >
    > > '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.


    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.

    --
    John
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
    John B. Matthews, Aug 8, 2005
    #10
  11. John B. Matthews wrote:
    > In article <D5IJe.2988$>,
    > Patricia Shanahan <> wrote:

    ....
    >>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?


    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
    Patricia Shanahan, Aug 8, 2005
    #11
  12. "" <> writes:

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


    No, Java cannot do ranges in cases, unlike e.g. Pascal.
    Tor Iver Wilhelmsen, Aug 8, 2005
    #12
  13. Guest

    whatabout
    switch(var){
    case 1:
    case 2:
    case 3: doSoemthingforOneToThree();
    break;
    ....
    , Aug 12, 2005
    #13
  14. "" <> writes:

    > 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.
    Tor Iver Wilhelmsen, Aug 13, 2005
    #14
  15. On 2005-08-13, Tor Iver Wilhelmsen penned:
    > "" <> writes:
    >
    >> 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.


    Are switches expensive?

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Aug 16, 2005
    #15
  16. Roedy Green Guest

    On Mon, 15 Aug 2005 17:14:38 -0600, "Monique Y. Mudama"
    <> wrote or quoted :

    >
    >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
    Roedy Green, Aug 16, 2005
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    441
  3. Nathan Sokalski
    Replies:
    2
    Views:
    1,586
    Nathan Sokalski
    Aug 9, 2008
  4. J.Ram
    Replies:
    7
    Views:
    650
  5. Nathan Sokalski
    Replies:
    2
    Views:
    1,319
    Nathan Sokalski
    Aug 9, 2008
Loading...

Share This Page