-1

For our java class, we have to format text using L allign, Centered, and R allign.

I have figured out how to save the string into a row, but i am not sure how to reset the row.

Any help would be great!

public static String word(String text, int width) {

    String word = "";
    String all = "";
    String row = "";
    String newRow = "";
    int textlen = text.length();

    for (int i = 0; i < text.length(); i++) {
        char current = text.charAt(i);
        // works gives a word
        do {
            if (text.charAt(i) != ' ') {

                word += text.charAt(i);
            }
            i++;

        } while (text.charAt(i) != ' ' && i < textlen);
        // till here
        word += " ";
        // System.out.println("this is word: " +word);

        while (row.length() < width) {
            if (word.length() < width
                    && (row.length() + word.length()) < width) {
                row += word;
                if ((width - (row.length() + word.length())) < 3) {
                    System.out.println("here is row: " + row);
                }

            }
            if (word.length() > width
                    && (row.length() + word.length()) > width) {

            }
            word = "";
        }
        row = "";
    }

    return word;
}
4

1 回答 1

0

如果要将String变量重置为空字符串,只需将其分配给空字符串:

row = "";
于 2013-11-15T02:05:30.333 回答