• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching using Scanner class exp. in K&B SCJP6

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


reply
    Bookmark Topic Watch Topic
  • New Topic