java - 如何将void语句转换为返回字符串的return语句?

标签 java apache-pig

好吧,所以我尝试将下面的语句转换为返回finalString的返回语句,但它总是不断地告诉我,即使我确实返回了finalString“这个语句 必须返回 String 类型的变量”。我尝试将 return FinalString 放入每个单独的 if 语句中、for 语句中、其外部,但它不起作用。我将非常感谢任何帮助或建议。 [更新代码]仍然不起作用。 FinalString 值不会被修改 if 语句,这正是我想要它做的。我想也许finalString值没有经过if语句?

[代码]

import java.util.Scanner;
public class pLat//pig latin program
{

    /**
       * Method to test whether a character is a letter or not.
       * @param c The character to test
       * @return True if it's a letter
       */
      private static boolean isLetter(char c) {
        return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
      }


    ///////////////////////////////////////////
      private static String output(String input)//processes the word using basic rules including the q and u rule
      {

          //the string that will hold the value of the word entered by the user
          char s;//the first character of the string
          char m;
          int l = input.length();//determines the length of the string
          String endString;
          String startString;
          String finalString = ""; //the final output
          String mtr;
          String lowercase;//the entered string all converted to lowercase

          for(int k =0;k<l;k++)//checks all letters in order to see which is a vowel
          {

              s = input.charAt(k);

          if(s == 'q'|| s=='Q' && input.charAt(k+1)=='u')//if the first vowel is a "u" and the letter before it is a "q"
          {


                  endString = input.substring(0,k+2);//makes the endString also include u
                  endString = endString +"ay";
                  startString = input.substring(k+2,l);
                  finalString = startString + endString;
                  //System.out.println(finalString);
                  return finalString;


          }

          if(s=='a'||s=='e'||s=='i'||s=='o'||s=='u'||s=='A'||s=='E'||s=='I'||s=='O'||s=='U'||s=='y'||s=='Y')//if its a vowel or "y" than executes commands below
          {

              endString = input.substring(0, k);//gets the letters before the vowel
              endString = endString + "ay";
              startString = input.substring(k,l);//gets the letters after the vowel
              finalString = startString + endString;
              //System.out.println(finalString);//prints the final result which is the combination of startString with endString
              //stops code after doing the above
              return finalString;

          }


          else if(k==l-1)//if its the end of the word
          {
              finalString = "ERROR";
              return finalString;

          }

         }
          System.out.println(finalString);
          return finalString;
}///////////////////////////////////

//   public static void process(String input)//will take care of the punctuation
//   {
//       String latin = "";
//          int i = 0;
//          while (i<input.length()) {
//
//            // Takes care of punctuation and spaces
//            while (i<input.length() && !isLetter(input.charAt(i))) {
//              latin = latin + input.charAt(i);
//              i++;
//            }
//            latin = latin + output(input);
//            System.out.println(latin);
//          }
//          
//    }


    public static void main(String[] args)
    {

        String str;//this will be the input string by the user
        Scanner scanner = new Scanner(System.in);//this scanner will register the input value
        System.out.println("Enter a Word: ");
        str = scanner.next();//stores the input string

        output(str);//outputs it using basic gramatical rules

    }

}

最佳答案

您的方法中的每个顶级本地 block 中都应该有一个return语句。如果您没有它,则在该顶级 block 内的每个 block 中都有一个return语句。等等。

让我们考虑一组最简单的情况 if - else if - else: -

您需要从每个 ifelse block 内返回字符串,因为它们中只有一个会执行。因此,如果您错过了其中一个 block 中的 return 语句,那么当执行该 block 时,它很可能会错过 return 语句。前提是您的方法末尾没有任何 return 语句

因此,基本上,您的 return 语句必须出现在每个 block 中,该 block 的执行不需要执行任何其他 block ,并且如果这些 block 涵盖了条件可能具有的所有可能性,那么您不需要 block 之外的 return 语句。因为这些 block 之一肯定会执行。

此外,如果这些 block 没有涵盖特定条件的所有可能性(例如,如果您没有else用于一组if-else-if),那么您必须在这些 block 之外有一个return语句。因为,如果这些 block 都不执行,则方法将错过 return 语句。

所以,例如您可以看到以下一组代码,涵盖了最可能的可能性:-

public String returnString() {
        if (..) {
             return "someString";

        } else if (...) {
             return "someString";

        } else {
             return "someOtherString";
        }
       // return statement here is not needed. Because at least `else` will execute
}

因此,至少 ifelse ifelse 中的一个将始终执行。因此,您可以在其中添加 return 语句,并将 return 语句保留在这些 block 之外。

但是,如果您的最后一个 else block 是 else if,那么可能没有一个 block 执行。在这种情况下,您必须在这些 block 后面放置一个 return 语句。

public String returnString() {
        if (..) {
             return "someString";

        } else if (...) {
             return "someString";

        } else if (...){
             return "someOtherString";
        }
       // return statement here is needed. 
       // Because its possible that none of the blocks in `if-else` set get executed.
}

另一种可能性是,您可以将返回值存储在某个局部变量中,而不是从每个 block 返回,然后在所有 block 的末尾,返回局部变量的作为方法中的最后一条语句。

public String returnString() {
    int returnValue = 0;
    if (..) { returnValue = someValue; }
    else if(...) { returnValue = someOtherValue; }

    return returnValue;
}

注意:-您可以在代码中使用最后一种方法,因为您将返回值存储在finalString中。因此,只需在方法的最后一行返回该字符串即可。

关于java - 如何将void语句转换为返回字符串的return语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125807/

相关文章:

hadoop - pig 用线程运行

与处理 int long 的 ORDER 相关的 HADOOP PIG 错误

java - 如何验证 EC2 中的应用程序正在使用 VPC 端点与 Dynamodb 进行通信?

java - 一次对 tomcat 和 wildfly 进行 Gradle war 依赖

java - Apache Httpclient 中止请求导致错误

java - apache struts2中如何获取请求体和响应体? - 使用 $.ajax 发布

在hadoop中加入文件A、B、C

hadoop - 在 Pig-Latin 中的 FOREACH 之后使用 FILTER 失败

java - 在哪里可以找到 OpenJDK 测试?

hadoop - Foreach inside pig 中的 Foreach