• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use StringTokenizer class with delimiter as space.
 
prasad kakani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sanghmitra Adhage wrote:You can also use StringTokenizer class with delimiter as space.



how can i do that???

 
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sanghmitra Adhage
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!