Java help
Hello,
I'm kinda new to Java but I like it and it is a lot of fun. I got some trouble finding out how to print out the first and last word in an entered sentence. Say you have a JOptionPane.showInputDialog coming up for the user, the user enters a sentence of a minimum of 2 words. When he hits "Ok" I want the first word and last word to be displayed in the same JOptionPane.showMessageDialog. Ex sentence. Hello world First word: Hello Last word: world I was really trying on my own but did not get that far, I was able to use this on one set String. Here is my code, please help me get so I can type my own sentence. PHP Code:
|
Re: Java help
You need to replace the existing string with a call to JOptionPane.showInputDialog, which returns the input as a String.
Something like PHP Code:
|
Re: Java help
Look up the String split() function: it splits a string into an array of smaller strings, based on a certain delimiter (basically where you want to separate the strings from). You can make it so that it splits it based on a space, so you will get an array of words, in your case. Then take the 0 index and the length-1 index of that array, and those will give you the first and last words entered.
|
Re: Java help
Quote:
|
Re: Java help
Quote:
|
Re: Java help
Quote:
PHP Code:
|
Re: Java help
Wow, I really had a blonde moment in my previous post, didn't I?
|
Re: Java help
Thanks nikilgupta345
EDIT: nikilgupta345 you should be able to type in your own sentence, not put a sentence in a String. (JOptionPane.showInputDialog) |
Re: Java help
PHP Code:
|
All times are GMT -4. The time now is 14:28. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.