Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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
Posts: 23820
140
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.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!