Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on scnner class.

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pranav,but always I got at last whether the input is

dsfs12fsf2f1

OR

dsfs12fsf2f1S
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ashok", please check your private messages for an important administrative matter.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same output as Ashok.
(OpenJDK 6)
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic