• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read primitives like integer from console  RSS feed

 
Gaurav Kalra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls help me as to how we can read primitive types like int from the console. Assuming i try to enter 24 from the console.
Gaurav
 
sai challa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=new String();
line=br.readLine();
//if you enter 24 you can retrieve it by using the method //Integer.parseInt(String)
int number=Integer.parseInt(line.trim());//you should use //line.trim() to get rid of any leading or trailing whitespace
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!