File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Searching using Scanner class exp. in K&B SCJP6 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 "Searching using Scanner class exp. in K&B SCJP6" Watch "Searching using Scanner class exp. in K&B SCJP6" New topic
Author

Searching using Scanner class exp. in K&B SCJP6

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Hi ,

Can anyone please explain the program below,

package regex;
import java.util.*;
public class ScanIn {

public static void main(String[] args) {
System.out.println("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 e){
e.printStackTrace();
System.out.println("scan exc");
}
}

}


with the following invocation and input : java ScanIn "\d\d"
input : 1b2c335f456

the output is supposed to be: found 33
found 45
found null

but iam getting an ArrayOutOfBoundsException at line 12, so my output is found null
can anyone help me correct the problem. and also explain the program?
I dont understand what these lines in the program actually do,

Scanner s = new Scanner(System.in);
token=s.findInLine(args[0]);


SCJP 6.0 90%
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Please place your code in the CODE tags.

Are you sure you passed the arguments ? args appears to have no values. This is what I can infer from the exception thrown


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Anuradha Prasanna wrote:Hi ,

Can anyone please explain the program below,

package regex;
import java.util.*;
public class ScanIn {

public static void main(String[] args) {
System.out.println("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 e){
e.printStackTrace();
System.out.println("scan exc");
}
}

}


with the following invocation and input : java ScanIn "\d\d" <= The meaning of "\d\d" find one digit followed by another digit.
input : 1b2c335f456
<= output for this would be "33" and "45".
the output is supposed to be:
found 33
found 45
found null
<= its showing null because you are using do while loop, in do while its print first and check condition later, for detail take a look on following code.



but iam getting an ArrayOutOfBoundsException at line 12, so my output is found null
can anyone help me correct the problem. and also explain the program?
I dont understand what these lines in the program actually do,

Scanner s = new Scanner(System.in);
token=s.findInLine(args[0]);




public String findInLine(String pattern)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#findInLine%28java.lang.String%29


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching using Scanner class exp. in K&B SCJP6