Search code examples
javastringappendconcatenation

Concatting Strings Across Functions


I'm working on a program that will take a text file and convert numbers from 0 - 99 to words (i.e 0 -> "zero", 99 -> "ninety nine"). I am able to convert the numbers and append them to the string within one function but the final sting output does not contain any of the converted numbers. Here is the code:

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:
        https://stackoverflow.com/questions/28643680/java-storing-each-word-from-an-input-file-in-an-array-of-strings */
        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: https://stackoverflow.com/questions/18590901/check-if-a-string-contains-numbers-java
            if(list.get(i).matches(".*\\d.*"))
            {
                convertToWord(list.get(i).toCharArray(), fullString);
            }
            else {
                fullString += (list.get(i) + " ");
            }
        }
        System.out.println(fullString);
    }
}

and here is a couple lines of the output(each - is a different system.out line):

-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

and here is those three lines on the input file:

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

As you can see the number is appended and printed with the system.out from within the function convertToWord but is lost once I go back to main. How can I append these conversions to the string across functions?


Solution

  • I can think of two ways of doing this:

    If you want to return the value of fullString to your main method, I suggest you modify convertToWord so that it has return type String, like this:

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

    Then, the method needs to return fullString instead of returning nothing.

    And finally, assign the return value or convertToWord in your main method to a variable and you can use it as you want in your main.

    Alternatively, you can declare fullString as a StringBuffer, and use the method append() to append to it.