• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner

 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Scanner we have methods nextInt(), nextFloat().

What is the exact purpose of these methods we have next() method to use rite.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This methods actually works on Regular expression , If your input which you expect to be a series of integers, then you can scan them using nextInt() , which definitely returns only int and rest of input type is ignored , same for nextFloat(),

and

next() is kind of generics !, It can take any kind of input no matter , what you scan !

This topic helps you to understand this concept better !

Hope this Helps !
[ July 31, 2008: Message edited by: Sagar Rohankar ]
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..

If My iput is "abc 2000 xyz 5000".

If I use nextInt() 1st, what happens can't it find directly 2000 instead of "abc"?

Becuase abc is a string so I thought I will retrun next int i.e 2000. But it is thowing exception.

Please expain me nit clear about nextInt().
[ August 01, 2008: Message edited by: Chintu sirivennela ]
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Chintu! you are not checking firstly for the occurrence of an int
Please use :
if(scanner.hasNextInt()){
int i=scanner.nextInt();
...
}
If you simply use next(), it'll give you everything with space as a default delimiter
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nextInt() takes the next token and tries to convert it into an integer. If the token doesn't represent an int, an exception is thrown. Therefore, before using nextInt() you should apply hasNextInt(). This method checks whether the next token really represents an int.
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for good response
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic