我試圖建立一個搜索按鈕。點擊搜索按鈕後,它將從JComponent outTextArea中讀入文本。掃描,字符串和文本區域的IProblems
它會讀取每個單詞,並將每個讀入單詞與我正在搜索的單詞進行比較。我的問題是,它的工作原理很簡單。它只讀取outTextArea中的最後一個單詞。
這是代碼片段
if(e.getActionCommand().equals("Search"))
{
String strSearchString = searchTextField.getText();
System.out.println(strSearchString);
String text = outTextArea.getText();
System.out.println(text);
Scanner sc = new Scanner(text);
while(sc.hasNext() == true)
{
String s = sc.next();
if (s.equals(strSearchString) == true)
{
searchOutLabel.setText("Yes");
}
else
{
searchOutLabel.setText("Non!");
}
}
sc.close();
}
如果我添加休息;否則,它會搜索第一個單詞。所以它告訴我,我的邏輯必須有所缺陷,而且不能這樣做。
不,這不是我想到的。我希望它檢查從TextArea掃描的整個文本。我只想知道在另一個TextField中輸入的搜索詞是否在掃描文本中。如果是,「是」,那不是「否」。 –
啊,那麼你的邏輯是關閉的,因爲它只會檢查每個單詞,並會很快完成。一旦遇到錯誤,您必須退出循環,這很簡單。 –
@NoCanDo:請參閱編輯 –