This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to read integers from keyboard? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to read integers from keyboard?" Watch "how to read integers from keyboard?" New topic

how to read integers from keyboard?

ramesh kumar
Ranch Hand

Joined: Nov 16, 2000
Posts: 32
Hi all
One of my course assignment wants me to read integers from
keyboard. i've tried InputStream and DataInputStream methods but
i am not able to do it. so please tell me how to read integer
from keyboard.
With regards
Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
I guess the first question is how the numbers are typed in. Are they typed in one per line or many per line. Haven't compiled this but this should work for one per line.
DataInputStream din = new DataInputStream(;
while((String s = din.readLine()) != null)
int tmpValue = Integer.parseInt(s);
If there are many per line look at using the StringTokenizer class.

[This message has been edited by Van Glass (edited November 19, 2000).]

Joined: Nov 14, 2000
Posts: 12
readLine() is deprecated with DataInputStream. The following can also be used instead :
System.out.println("Enter an Integer :");
BufferedReader br=new BufferedReader(new InputStreamReader(;
int i=Integer.parseInt(br.readLine());
System.out.println("Entered Integer is :"+i);
Nadeem Malik

Joined: Nov 22, 2000
Posts: 10
i think u should try this.
class useint{
public static void main (String args[])
throws IOException {

BufferedReader b = new BufferedReader
(new InputStreamReader(;
String string;
int i;
int sum = 0;
System.out.println("Please input numbers:");
System.out.println("Type 0 to exit");

string = b.readLine();
i = Integer.parseInt(string);
} catch(NumberFormatException NFE){
System.out.println("Please try again");
i = 0;

sum = sum + 1;
System.out.println("Count = " sum);
} while(i != 0);
Have you checked out Aspose?
subject: how to read integers from keyboard?
It's not a secret anymore!