我试图获取文本字段的值,并将其应用于方法,以便在文本文件中搜索该值.但在我的方法中,我显示了一个Missing Return Value错误,似乎无法使其正常工作.下面是我的代码:
submitsearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
whatToSearch = searchfield.getText();
result = SearchString(whatToSearch);
}
});
}
public String SearchString(String result)
{
String input;
try{
String details, id, line;
int count;
Scanner housenumber = new Scanner(new File("writeto.txt"));
while(housenumber.hasNext())
{
id = housenumber.next();
line = housenumber.nextLine();
{
if(!housenumber.hasNext())
JOptionPane.showMessageDialog(null,"No Properties with this criteria");
}
if(result.equals(id));
{
JOptionPane.showMessageDialog(null,id + line );
}
}
}
catch(IOException e)
{
System.out.print("File failure");
}
}
}
Run Code Online (Sandbox Code Playgroud)
附录:
在SearchString中,我希望在我的文本文件中搜索在我的文本字段中输入的值,将其显示在JOptionPane中.虽然我点击搜索时现在有了返回语句,但无论是否与我的搜索匹配,我都会逐一显示JOptionPanes中的所有记录