aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on scnner class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on scnner class." Watch "Doubt on scnner class." New topic
Author

Doubt on scnner class.

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

This program is from K&B page-484 ,When I run it:-
it produces this:-

java SearchTest "\d+"
input: dsfs12fsf2f1
found 12
found 2
found 1
found null

Someone tell me at last why it found null !!

import java.util.regex.*;
import java.util.*;
class SearchTest
{
public static void main(String[] args)
{
System.out.print("input:");
System.out.flush();

try
{
Scanner s = new Scanner(System.in);
String token;
do
{
token = s.findInLine(args[0]);
System.out.println("found "+token);
}
while(token!=null);


}
catch(Exception ex)
{
ex.printStackTrace();
}

}
}
Martti Toim
Greenhorn

Joined: Jul 13, 2008
Posts: 3
Hi,
the Java API describes findInLine
Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.


so if you would change your do {} while() loop to following, it would work correctly.



SCJP 5.0 @ 84%
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Ashok,
I tried to run your code but it didn't gave me any null. This is what i got-:

found 12
found 2
found 1


Actually the findInLine works like this-:
It tries to find the pattern in input. It search for pattern before the termination(i.e Enter key pressed) and returns the matched pattern else null will be returned. That why you didn't get null with dsfs12fsf2f1 as it finds 1 as last match. But if you give it something like this dsfs12fsf2f1S it will return null at last as it didn't find a match just before terminating. This dsfs12fsf2f1S gives output like this-:

found 12
found 2
found 1
found null


Hope this helps.
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks Pranav,but always I got at last whether the input is

dsfs12fsf2f1

OR

dsfs12fsf2f1S
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

"Ashok", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Ashok,
I re-run the code but again got the same output in java 5 compiler. Are you sure you don't have any space after dsfs12fsf2f1or the other one dsfs12fsf2f1S. And you are using the pattern as "\d+". It should give you what it gave to me.
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
I have the same output as Ashok.
(OpenJDK 6)


SCJP 6.0 (87%)
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi,
It seem that this behavior of findInLine() was some bug in jdk5 and have been changed in jdk6. Now for every line termination it will throw a null at last as it didn't got a match for it. The below links give some light on this-:
Link 1
Link 2
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks to all !!The behavior of the output matters in JDK 1.6 and JDK 1.5.and now I got it.Can someone can provide the links where can I find Console related topics that is in SCJP 6 objective.I don't have SCJP 6 book.

Thanks !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on scnner class.