缺少退货声明 [英] Missing Return Statement

查看:38
本文介绍了缺少退货声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取文本字段的值并将其应用于方法,以便在文本文件中搜索该值.但是在我的方法中,我显示了一个 Missing Return Value 错误,并且似乎无法使它工作.下面是我的代码:

i am attempting to take the value of a textfield and make apply it to a method in order to search a textfile for that value. But in my method i am shown a Missing Return Value error and cannot seem to make it work. below is my code:

        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");
      }
   }
}

附录:

在 SearchString 中,我希望在我的文本文件中搜索在我的文本字段中输入的值,并将其显示在 JOptionPane 中.虽然我现在在单击搜索时有返回语句,但我会一一显示 JOptionPanes 中的所有记录,无论它们是否与我的搜索匹配

In SearchString i am hoping to search my textfile for the value entered in my textfield display it in a JOptionPane. Although i now have the return statement when i click search i am shown all of the records in JOptionPanes one by one regardless of whether they match my search

推荐答案

在 catch 块之后添加一个 return null;.

Add a return null; after catch block.

方法签名说它返回一个字符串.这意味着无论您的代码采用什么流程,该方法都应该返回一个值.但是当异常发生时,没有返回.因此,您必须在发生异常时指定返回值

The method signature says that it returns a String. That implies no matter what flow your code takes, the method should return a value. But when an exception happens, there is no return. Hence you must specify a return value in the case when an exception happens

这篇关于缺少退货声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆