为什么我在Java中出现“缺少return语句”错误,即使我在第二类中的线程主错误中有return语句和异常?

时间:2015-03-27 12:45:10

标签: java

例如,我在这里收到错误 - 这只是一个片段。我在3个不同的操作员中得到了3次错误。

   public boolean delete(String name) {
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name)) {
            directory[i] = null;
            return true;
         }   
         else
            return false;
      }
   }

我在这里也有同样的错误:

   public boolean add(String name) {
      if (directory.length == 1024)
         return false;
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name))
            return false;
         else
            directory[directorySize++] = name;
            return true;
      }
   }          

然后在我的第二个类(用户界面部分)中,我不断收到此错误:线程“main”中的异常java.lang.NoClassDefFoundError:Directory

以下是该类的完整代码:

import java.io.*;
import java.util.*;
public class DirectoryWithObjectDesign {
   public static void main(String[] args) throws IOException {
   String directoryDataFile  = "Directory.txt";
   Directory d = new Directory(directoryDataFile);
   Scanner stdin = new Scanner(System.in);
   System.out.println("Directory Server is Ready!");
   System.out.println("Format: command name");
   System.out.println("Enter ^Z to end");
   while (stdin.hasNext()) {
      String command = stdin.next();
      String name = stdin.next();
      if (command.equalsIgnoreCase("find")) {
         if (d.inDirectory(name))
            System.out.println(name + " is in the directory");
         else 
            System.out.println(name + " is NOT in the directory");
      }
      else if (command.equalsIgnoreCase("add")) {
         if (d.add(name))
            System.out.println(name + " added");
         else 
            System.out.println(name + " cannot add! " + "no more space or already in directory");
      }
      else if (command.equalsIgnoreCase("delete")) {
         if (d.delete(name))
            System.out.println(name + " deleted");
         else
            System.out.println(name + " NOT in directory");
      }
      else {
         System.out.println("bad command, try again");
      }
   }
   }
}   

这是我的目录类的代码:

import java.util.*;
import java.io.*;
public class Directory {
   //public static void main(String[] args) {
   final int maxDirectorySize = 1024;
   String directory[] = new String[maxDirectorySize];
   int directorySize = 0;
   File directoryFile = null;
   Scanner directoryDataIn = null;

   public Directory(String directoryFileName) {
      directoryFile = new File(directoryFileName);
      try {
         directoryDataIn = new Scanner(directoryFile);
      }
      catch (FileNotFoundException e) {
         System.out.println("File is not found, exiting!" + directoryFileName);
         System.exit(0);
      }
      while (directoryDataIn.hasNext()) {
         directory[directorySize++] = directoryDataIn.nextLine();
      }
   }
   public boolean inDirectory(String name) {
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name)) 
            return true;
         else 
            return false;
      }
   }
   public boolean add(String name) {
      if (directory.length == 1024)
         return false;
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name))
            return false;
         else
            directory[directorySize++] = name;
            return true;
      }
   }          

   public boolean delete(String name) {
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name)) {
            directory[i] = null;
            return true;
         }   
         else
            return false;
      }
   }

   public void closeDirectory() {
      directoryDataIn.close();
      PrintStream directoryDataOut = null;
      try {
          directoryDataOut = new PrintStream(directoryFile);
      }
      catch (FileNotFoundException e) {
         System.out.printf("File %s not found, exiting!", directoryFile);
         System.exit(0);
      }
      String originalDirectory[] = {"Mike","Jim","Barry","Cristian","Vincent","Chengjun","susan","ng","serena"};
      if (originalDirectory == directory)
         System.exit(0);
      else
         for (int i = 0; i < directorySize; i++)
            directoryDataOut.println(directory[i]);
         directoryDataOut.close();
   }
}

1 个答案:

答案 0 :(得分:4)

重点是编译器无法知道你的for循环是否会被输入。因此,您需要在for循环结束后之后返回。换句话说:在您的方法中可能采用的任何路径都需要最终的return语句。实现这一目标的一个简单方法是只有一个返回语句;并把它放在方法的最后一行。这看起来像是:

 Object getSomething() {
   Object rv = null; // rv short for "returnValue"
   for (int i=0; i < myArray.length; i++) {
     if (whatever) {
       rv = godKnowsWhat;
     } else {
       rv = IdontCare;
     }
   }
   return rv;
 }

在你的第二个例子中,缩进似乎表明你在else语句中有一个返回

        directory[directorySize++] = name;
        return true;

但是当你仔细观察时,你会发现在else

之后有两个语句
 else
        directory[directorySize++] = name;
        return true;

所以这实际上就像是

 else
        directory[directorySize++] = name;
 return true;

含义:始终将{braces}放在所有块周围,即使是(假设)单线然后/其他线。这有助于避免这样的错误,当一个班轮变成两个班轮时(反之亦然; - )

“NoClassDefFoundException”意味着:在指定给java的类路径中......没有类Directory.class

要解决这个问题,你应该研究java类路径的内容;以及如何正确设置。