我试图找到在句子中输入的第二个单词。我已经确定了第一个词,但我很难找到如何获得第二个词。这是我尝试过的:
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);