• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching using Scanner class exp. in K&B SCJP6

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic