Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2008, 08:00 PM
Member
 
Join Date: Nov 2007
Posts: 50
sireesha is on a distinguished road
Tokenizing with Scanner
Hi all,
Please check this code..
Code:
import java.util.Scanner; public class Scanner_Tokenizing { public static void main(String[] args) { System.out.println("Enter some string:"); Scanner s=new Scanner(System.in); String st; int count=0; boolean b=false; while(b=s.hasNext()) { st=s.next(); count++; System.out.println(st); } System.out.println("Total number of tokens : "+count); } }
While running this code..it just prints all the tokens and continuously running..
It never came to the output statement after while loop..
What's wrong with my code ?
Please tell me.
Thanq very much for reading this.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 08:56 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Very challenging
Hello sireesha

This was very challenging for me (no IDE used ), but I have a solution!
Code:
import java.util.Scanner; import java.io.*; public class Scanner_Tokenizing { public static void main(String[] args) { try{ System.out.println("Enter some string:"); DataInputStream input = new DataInputStream(System.in); String text = input.readLine(); Scanner scanner = new Scanner(text); int count = 0; while (scanner.hasNext() == true){ String st = scanner.next(); count++; System.out.println(st); } System.out.println("Total number of tokens : " + count); } catch (Exception e){ System.out.println(e.getMessage()); } } }
The fragments in blue are the important ones. Remember that the System.in input stream can be infinitively long. So, the scanner does not know that a new line symbol means stop. That's why I read the input stream with a DataInputStream object before hand, to obtain a finite String object to work with. Also remember that when you compare stuff, you must use "=="

I hope this helps!
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 02-05-2008 at 09:04 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 10:36 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { System.out.println("Enter some string:"); Scanner s=new Scanner(System.in); int count=0; while(s.hasNext()) { String st = s.nextLine(); if(st.equalsIgnoreCase("x")) break; String[] tokens = getTokens(st); System.out.printf("tokens = %s%n", Arrays.toString(tokens)); count++; } System.out.println("Total number of tokens : "+count); s.close(); } private static String[] getTokens(String str) { System.out.println("str = " + str); Scanner scanner = new Scanner(str); //System.out.println("delimiter = " + scanner.delimiter()); String[] s = new String[str.length()]; // Capture the next word character, space or puntuation mark. String regex = "(?=[\\w\\s\\p{Punct}])"; Pattern pattern = Pattern.compile(regex); scanner.useDelimiter(pattern); //System.out.println("delimiter = " + scanner.delimiter()); int count = 0; while(scanner.hasNext("[\\w\\s\\p{Punct}]")) { s[count++] = scanner.next("[\\w\\s\\p{Punct}]"); } scanner.close(); return s; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-05-2008, 10:44 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Excellent
Thanks hardwired. I did not think of using regular expressions.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scanner class ajaymenon.k Advanced Java 1 11-26-2007 09:01 AM
Using ava.util.Scanner Java Tip Java Tips 0 11-20-2007 06:47 PM
Using the scanner method silvia Advanced Java 1 08-07-2007 07:50 AM
help with IP scanner tommy New To Java 1 08-06-2007 10:00 PM
JDK 5.0 Scanner Class Sircedric88 New To Java 3 07-27-2007 08:55 PM


All times are GMT +3. The time now is 12:50 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org