| Author |
Scanner
|
Amruth Puppala
Ranch Hand
Joined: Jul 14, 2008
Posts: 295
|
|
In Scanner we have methods nextInt(), nextFloat(). What is the exact purpose of these methods we have next() method to use rite.
|
SCJP 5 |SCWCD 5| Started thinking about Web Services ?
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
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 ]
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Amruth Puppala
Ranch Hand
Joined: Jul 14, 2008
Posts: 295
|
|
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
Joined: Jul 02, 2008
Posts: 310
|
|
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
Joined: Apr 27, 2008
Posts: 342
|
|
|
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.
|
SCJP 5 (98%) - SCBCD 5 (98%)
|
 |
Amruth Puppala
Ranch Hand
Joined: Jul 14, 2008
Posts: 295
|
|
|
Thanks for good response
|
 |
 |
|
|
subject: Scanner
|
|
|