Code with the error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Code with the error

Hybrid View

  1. #1
    anu Guest

    Code with the error


    This is the code thats causing the problem. It is prety straightforward.


    import java.io.*;

    public class Mys
    {
    public static void main( String args[] ) throws IOException
    {
    int x;

    System.out.print( "Enter value for x: ");
    System.out.flush();
    x = System.in.read();
    System.in.skip(1);


    System.out.println( "x = " + x );

    }

    }

  2. #2
    Paul Clapham Guest

    Re: Code with the error

    Yes, that's what's happening. System.in does return an int but the Java
    documentation says: Reads the next byte of data from the input stream. It's
    reading a byte and casting it to an int. Try running the program and
    entering things that aren't digits. It won't throw an error, it will just
    give you the Unicode value of the characters you type.

    How to get what you want? Here's some code that works (Java doesn't make it
    easy to get input from the console!)

    char ca[] = {(char)x};
    int i = Integer.parseInt(new String(ca));
    System.out.println( "x = " + i );


    anu <arunaa@hotmail.com> wrote in message news:38db9e87$1@news.devx.com...
    >
    > This is the code thats causing the problem. It is prety straightforward.
    >
    >
    > import java.io.*;
    >
    > public class Mys
    > {
    > public static void main( String args[] ) throws IOException
    > {
    > int x;
    >
    > System.out.print( "Enter value for x: ");
    > System.out.flush();
    > x = System.in.read();
    > System.in.skip(1);
    >
    >
    > System.out.println( "x = " + x );
    >
    > }
    >
    > }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center