Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

how to read integers from keyboard?  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ramesh
------------------
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(System.in);
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).]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
readLine() is deprecated with DataInputStream. The following can also be used instead :
System.out.println("Enter an Integer :");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in);
int i=Integer.parseInt(br.readLine());
System.out.println("Entered Integer is :"+i);
Kiran
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i think u should try this.
import java.io.*;
class useint{
public static void main (String args[])
throws IOException {

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

do{
string = b.readLine();
try{
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);
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!