Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence."
Assume that there is another variable declared, secondWord , also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord.
I should also note that I'm supposed to use indexOf and substring, and what I'm having trouble is finding the ending value for the substring.
Here is what I have:
int position = sentence.indexOf(" ");
secondWord = sentence.substring(position+1, );
The logic behind the code is that since I need the second word I look for the first space character in the string then for the substring I add 1 to "position" so I skip the space and start at the word, the problem is that I don't know what value I need to put for the ending because it can't be a set value since each string might be different.

New Topic/Question
Reply



MultiQuote




|