Results 1 to 4 of 4
  1. #1
    Dangi is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default throwing bug value

    Hi People, this program throwing bug value. it should print out whatever i give it as input.
    anything woring i am doing?

    public class Hello {
    public static void main( String args[])
    {


    int j;
    try {
    j = System.in.read();
    System.out.print(j);
    } catch (IOException e) {

    System.out.println("error");
    }}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    read() is not doing what you want there. It's read the next cumming/finding byte of a stream. That byte print as an int value, so that's why you get a bug value. Read the Java docs for more details.

    To read the user inputs use Scanner class.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    it should print out whatever i give it as input.
    It is. All the keys on a keyboard send an integer to the input handlers. To get that integer as a character, you need to use some conversion program/technique to convert it to a char or String.
    For example the integer value of the character 'a' is 0x61 0r 97. Is that what you see with your program when you type in an 'a' (without 's)?
    To convert an int to a char, you can cast it:
    println("j=" + (char)j);

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Like this.

    Java Code:
            int j;
            try {
                j = System.in.read();
                System.out.println((char)j);
            }
            catch(Exception e) {
                e.printStackTrace();
            }

Similar Threads

  1. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 03:34 PM
  2. Display Message Without Throwing Exception
    By kailashchandra in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-27-2008, 10:05 AM
  3. Replies: 2
    Last Post: 06-02-2008, 11:24 PM
  4. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 10:35 PM
  5. Which statement is throwing a runtime error....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 01:55 PM

Posting Permissions

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