跨函数连接字符串

Concatting Strings Across Functions

我正在开发一个程序,它将获取一个文本文件并将数字从 0 - 99 转换为单词(即 0 -> “零”,99 -> “九十九”)。我能够转换数字并将它们附加到一个函数中的字符串,但最终的字符串输出不包含任何转换后的数字。这是代码:

static ArrayList<String> list;

    static void convertToWord(char[] num, String fullString)
    {
        //number to word : https://www.geeksforgeeks.org/convert-number-to-words/
        String[] single_digits = new String[]{ "zero", "one",
                "two", "three", "four",
                "five", "six", "seven",
                "eight", "nine"};
        String[] two_digits = new String[]{"", "ten", "eleven", "twelve",
                "thirteen", "fourteen",
                "fifteen", "sixteen", "seventeen",
                "eighteen", "nineteen"};
        String[] tens_multiple = new String[]{"", "", "twenty", "thirty", "forty",
                "fifty","sixty", "seventy",
                "eighty", "ninety"};

        int len = num.length;

        if (len == 0)
        {
            System.out.println("Error");
            return;
        }

        if (len == 1)
        {
            fullString += single_digits[num[0] - '0'];
            return;
        }

        if (len > 2)
        {
            System.out.println("No numbers greater than 99");
            return;
        }

        int x = 0;
        while (x < num.length)
        {
                if (num[x] - '0' == 1)
                {
                    int sum = num[x] - '0' +
                            num[x] - '0';
                    fullString += two_digits[sum];
                    return;
                }

                else if (num[x] - '0' == 2 &&
                        num[x + 1] - '0' == 0)
                {
                    fullString += "twenty";
                    return;
                }

                else
                {
                    int i = (num[x] - '0');
                    if(i > 0)
                        fullString += tens_multiple[i]+" ";
                    else
                        fullString += "";
                    ++x;
                    if (num[x] - '0' != 0)
                        fullString += single_digits[num[x] - '0'];
                }
            ++x;
        }
    System.out.println(fullString);
    }

    public static void main(String[] args) throws FileNotFoundException
    {
        /* read each element of file as a separate member of array:
         */
        String fullString = "";
        Scanner s = new Scanner(new File("C:/Users/Zach/Desktop/Fall 2020/Java_Kotlin/java-kotlin-zpheesaker/KattisNumberstoWords/app/input.txt"));
        list = new ArrayList<String>();
        while(s.hasNext())
        {
            String token = s.next ();
            list.add(token);
        }

        for (int i = 0; i < list.size(); i++)
        {
            // check if number by regex: 
            if(list.get(i).matches(".*\d.*"))
            {
                convertToWord(list.get(i).toCharArray(), fullString);
            }
            else {
                fullString += (list.get(i) + " ");
            }
        }
        System.out.println(fullString);
    }
}

这是几行输出(每一行都是不同的 system.out 行):

-Piqued favour twenty three
-Piqued favour stairs it enable exeter as seeing. Remainder met improving but forty one
-Piqued favour stairs it enable exeter as seeing. Remainder met improving but engrossed sincerity age. Better but seventy eight

这是输入文件中的那三行:

Piqued favour 23 stairs it enable exeter as seeing.
Remainder met improving but 41 engrossed sincerity age.
Better but 78 length denied abroad are.

如您所见,该数字在函数 convertToWord 中附加并使用 system.out 打印,但一旦我返回 main,该数字就会丢失。如何跨函数将这些转换附加到字符串?

我可以想到两种方法:

如果你想return fullString 的值到你的 main 方法,我建议你修改 convertToWord 使其具有 return 类型String,像这样:

static void convertToWord(char[] num, String fullString)

然后,该方法需要 return fullString 而不是 return 什么都不做。

最后,将 main 方法中的 return 值或 convertToWord 分配给一个变量,您可以在 main.

中随意使用它

或者,您可以将 fullString 声明为 StringBuffer,然后使用方法 append() 附加到它。