Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

HELP!!

Thread view: 
Michael - 28 Sep 2005 04:56 GMT
Ok guys, I've tried everthing thing I can think of. I even went to see the
teacher for help. I am almost there I can feel it. I just can't get the last
word to return. If anyone could help that would be great and much
appreaciated
public class split {

public static void main(String [] args){

System.out.println("Please enter 3 words, followed by a space between each
word. ");

String input;
input = Stdin.readln(); // Read input from console.

int blank1 = input.indexOf(" ");
String word1= input.substring(0, blank1); // Finds first word in sentence.

String rest = input.substring(blank1+1, input.length());//** both words on
new line*****

int blank2 = rest.indexOf(" ");
String word2 = rest.substring(0, blank2); // Finds second word in sentence.

String foo = input.substring(blank2+1,input.length());

String word3= foo.substring(0);

System.out.println("\n" + word1+ "\n" + word2 + "\n" + word3);
jessu - 28 Sep 2005 05:06 GMT
Replace the last few lines:

>>String foo = input.substring(blank2+1,input.length());
String foo = rest.substring(blank2+1,rest.length());
>> String word3= foo.substring(0);
This is not needed... foo contains the last word

----------
FeedFeeds : A new way to read news and blogs!
http://www.feedfeeds.com
Hal Rosser - 30 Sep 2005 01:37 GMT
Take a look at the split method of String
it may help and could be a little less clumsy.
hope this helps
Roedy Green - 30 Sep 2005 07:04 GMT
>Take a look at the split method of String
>it may help and could be a little less clumsy.
>hope this helps

The split method in String necessarily compiles the pattern every time
you use it.  If you use the Pattern version you need compile it only
once in a static init. Compiling a pattern is a non-trivial operation.
It is hardly any more work to do it properly.  It also lets you put
all your patterns side by side in the declarations  where there are
easy to compare and maintain.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.