文に入力された 2 番目の単語を見つけようとしています。最初の単語を決定しましたが、2 番目の単語だけを取得する方法を見つけるのに苦労しています。これは私が試したことです:
String strSentence = JOptionPane.showInputDialog(null,
"Enter a sentence with at" + " least 4 words",
"Split Sentence", JOptionPane.PLAIN_MESSAGE);
int indexOfSpace = strSentence.indexOf(' ');
String strFirstWord = strSentence.substring(0, indexOfSpace);
/*--->*/String strSecondWord = strSentence.substring(indexOfSpace, indexOfSpace);
Boolean blnFirstWord = strFirstWord.toLowerCase().equals("hello");
Boolean blnSecondWord = strSecondWord.toLowerCase().equals("boy");
JOptionPane.showMessageDialog(null, "The sentence entered: " + strSentence
+ "\nThe 1st word is " + strFirstWord
+ "\nThe 2nd word is " + strSecondWord
+ "\nIs 1st word: hello? " + blnFirstWord
+ "\nIs 2nd word: boy? " + blnSecondWord);