Results 1 to 6 of 6
  1. #1
    hitesh_public is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default why this doesn't work?

    Hello friends, I am very new to programming. I want to know why this code doesn't work.

    Java Code:
    void linkedListMenu() 
      throws IOException 
      {
        int c;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        do {
          System.out.println("Choose one of the following options:");
          System.out.println("\t1.Create the Linked List");
          c = br.read();
          System.out.println("how" + br.read() + "are you");
        } while(c != 1);
      }


    But when i use this form this works:

    Java Code:
    void linkedListMenu() 
      throws IOException 
      {
        char c;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        do {
          System.out.println("Choose one of the following options:");
          System.out.println("\t1.Create the Linked List");
          c = (char) br.read();
          System.out.println("how" + br.read() + "are you");
        } while(c != '1');
      }
    }
    Last edited by Fubarable; 08-08-2010 at 11:53 AM. Reason: Moderator edit: code tags added

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Please describe "doesn't work", in detail.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Moderator edit: italics removed, code tags added for readability. To learn how to do this yourself, please check out the links in my signature.

    Much luck!

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    You should really specify exactly what isn't working.

    However, I believe I see the problem. Read up on BufferedReader.read() and you should be able to figure it out too. BufferedReader (Java 2 Platform SE v1.4.2)

  5. #5
    Gahstanz is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    When after prompting for the user value and storing it into a variable, e.g
    System.out.println("\t1.Create the Linked List");
    c = br.read();

    This is trying to force a string value into an int variable.
    To implicitly cast the value into the variable you would write:
    c = Integer.parseInt(br.read());

    or explicit by

    c = (int) br.read();

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Gahstanz View Post
    When after prompting for the user value and storing it into a variable, e.g
    System.out.println("\t1.Create the Linked List");
    c = br.read();

    This is trying to force a string value into an int variable.
    To implicitly cast the value into the variable you would write:
    c = Integer.parseInt(br.read());

    or explicit by

    c = (int) br.read();
    No. The read method returns an int. (besides "casting" a String to an int, like your second example, wouldn't work any way)

    I have a feeling that the OP is complaining that the first time it prints a number rather than a character, and the second time it prints a character. This is perfectly understandable, since he is reading an int the first time a char (character) the second time, but until the OP describes exactly what his "problem" is this conjecturing is useless and probably counter-productive, when not outright wrong, like the previous post.

Similar Threads

  1. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM
  2. JMS work in JBOSS 5??
    By kkc85 in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 01-20-2010, 08:22 PM
  3. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  4. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 10:42 PM
  5. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •