Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Programmer Certification (OCPJP)
scann and split issue
Rashid Mian
Ranch Hand
Posts: 31
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am confused why no. of tokens in Scanner and Split are differnt(though using same regex).
Please explain in detail how these two different aproaches work to scan inputs.
import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; public class ScanIn{ public static void main(String[] args) { try { Scanner s = new Scanner("boo:and:foo"); s.useDelimiter("o"); String token=""; int i; boolean b; while(s.hasNext() ) { token= s.next(); System.out.println(">"+token+"<"); } // System.out.println(token); }catch(InputMismatchException e ){System.out.println("Excp Input mismatch"+e.getMessage());} catch(NoSuchElementException e ){System.out.println("Excp No Such element"+e.getMessage());} catch(IllegalStateException e ){System.out.println("Excp Illegabl"+e.getMessage());} } }
import java.util.*; class SplitTest { public static void main(String[] args) { String[] tokens = "boo:and:foo" .split("o" ) ; System.out.println("count " + tokens.length); for(String s : tokens) System.out.println(">" + s + "<"); } }
Jim Yingst
Wanderer
Posts: 18671
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I recommend you check the API for split(
String
) and compare it with the documentation for split(String, int). The answer lies there.
"I'm not back." - Bill Harding,
Twister
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
K & B example Problem.
for loop error
trouble using .useDelimiter
query with escape characters
Scanner findInLine going into infinite loop
More...