wood burning stoves*
The moose likes I/O and Streams and the fly likes read int inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "read int inputs" Watch "read int inputs" New topic
Author

read int inputs

shiva katte
Greenhorn

Joined: Feb 07, 2001
Posts: 2
import java.io.*;
public class demo {
public static void main(String s[]) throws IOException {
DataInputStream din=new DataInputStream(System.in);
int x,y,z;
System.out.println("Enter no ");
x=din.readInt();
System.out.println("Enter no ");
y=din.readInt();
z=x+y;
System.out.println("sum =" + z);
}
}
---> This code is not working as expected if i give nos as input
pl help
thanks
Neaman Shafiq
Greenhorn

Joined: Feb 07, 2001
Posts: 5
hi, try the following code instead - its similar to urs. there are so many ways of doing this. this one comes to mind.
import java.io.*;
public class demo
{
public static void main(String s[]) throws IOException
{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int x,y,z;
System.out.println("Enter no ");
x=Integer.parseInt(in.readLine());
System.out.println("Enter no ");
y=Integer.parseInt(in.readLine());
z=x+y;
System.out.println("sum =" + z);
}
}
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
hi,
The code which is writen by shiva doesn't have any problem .However the readInt() or writeInt(int) and some other methods are used when reading the data from the file or writing the data from and to the file.
If tried with files, code works fine . However when tried directly, without the file ,I think it reads the data in form Of bytes. That is why no proper output is seen.

------------------
Sandeep Jain


Try and Try Till u succeed<br /> <br />Sandeep Jain
gul khan
Greenhorn

Joined: Feb 14, 2001
Posts: 17
can any body explain(more elaborately) why that code did not work
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: read int inputs