Two Laptop Bag
The moose likes Java in General and the fly likes Reading String contains space using Scanner (Its too Urgent please!!!!) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Reading String contains space using Scanner (Its too Urgent please!!!!)" Watch "Reading String contains space using Scanner (Its too Urgent please!!!!)" New topic
Author

Reading String contains space using Scanner (Its too Urgent please!!!!)

prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59
Hello Friends!!

Please its too urgent..how to read the string which contains spaces...

See my code is as follows..

//Assume the input string is -----> 1 2 3


But the code is not working................... Please help out in this regard!!!

Thanks
Prasad
Sanghmitra Adhage
Greenhorn

Joined: Jun 26, 2009
Posts: 16
You can also use StringTokenizer class with delimiter as space.
prasad kakani
Ranch Hand

Joined: Jul 15, 2008
Posts: 59
Sanghmitra Adhage wrote:You can also use StringTokenizer class with delimiter as space.


how can i do that???

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

But the code is not working................... Please help out in this regard!!!



Well, first of all, you really need to define what you mean by "not working". But if I had to take a guess...

I am assuming that the code doesn't do anything when you type in the one line. The reason for that is because you are scanning stdin, which keeps reading, so you can't depend on the while loop to exit after the single line is read. Your only exit strategy, is if a non-int, followed by a space, is encountered.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sanghmitra Adhage
Greenhorn

Joined: Jun 26, 2009
Posts: 16
Please see following javadoc:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

This also has an example.

I suppose you are trying to read distinct words, delimited by space from a string input through console. This is also easy. You can use it if its urgent.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reading String contains space using Scanner (Its too Urgent please!!!!)
 
Similar Threads
MultiDimenaional Array sort
StackOverflowError(urgent)
urgent
Urgent !
Urgent....