File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read int inputs

 
shiva katte
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gul khan
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body explain(more elaborately) why that code did not work
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic