Originally posted by Mike Smith:
input=Stdin.readln(); read a sentence from the user.
but next I am unsure if I do need to convert the string into an integer before I can manipulate the string.For example, do I need this command,
word=Integer.parseInt(input); // to convert string into integer and assign it contents to the variable word...
Mike, it sounds like you are fairly new to this game. This is a good beginning programming exercise because it will force you to think about the algorithm. That is why you need to write the program using simple methods like length(), indexOf() and substring() [NOT subString()]. There are more powerful tools available, but you would not learn as much using them. You will learn some Java along the way as well, but that is secondary.
So before thinking about the Java programming, just think about the problem. Your program will be given a string containing a number of words, and it needs to identify each word (so it can print them out). Your tools are:length() will tell you how long the input String isindexOf(int ch) will find the FIRST space in the StringindexOf(int ch, int fromIndex) will find the NEXT space (and the NEXT ...)substring() can be used to get the word in between any two spaces
Step 1 for you is to forget about writing a Java program, and just work out in clear detail how you can use those tools to solve your problem. Imagine that you are playing a game, where another player has written down a string, but will not show it to you. You have to work out what the words are, but the only questions you can ask are length(), indexOf(int), indexOf(int, int) and substring(). Once you have worked out how to do that, write down the instructions you would give to someone else so that they could play the game for you.
Once you have done that, you have your algorithm, and are ready to start programming.
Oh, by the way, for now you should start with a simplified version of the problem:The string has no leading of trailing spacesThe string contains no double spacesThe string contains at least two words
Once you have that program working, you should then remove these constraints one at a time, and make sure that the program still works when the string contains leading spaces, sequences of spaces, trailing spaces, only one word, and even no words at all! BUT, don't bother about these more complex cases until you have the basic case working.
Finally, you are ready to start programming. BUT before you jump in, you need to make sure that you understand how these String methods work. They are a bit tricky for beginner programmers. I suggest you start by carefully reading the Java Doc
entries for the String class, and these particular methods. Then you should experiment with them one at a time, until you are confident that you understand them. Most importantly, remember that String indexes begin at 0, not 1, and so the index of the last char in the string is length()-1.
For example, write simple test programs to make sure that you understand what startements like the following will print:
Only once you can correctly predict the output of such statements every time are you ready to start coding.
I realize that this seems like a long and slow way to approach your problem. I can assure you that it is not. Your classmates who just start banging away at the keyboard with the standard trial-and-error approach will still be going once you are finished, they won't learn anywhere near as much, their programs won't be as good, and it is highly unlikely that their programs will deal correctly with non-standard inputs.
Good luck with the challenge.