{"id":"return-values-building-text","slug":"returning-text","title":"Returning text built in a loop","text":"A method can build a String in a loop and return it. Start from the empty String `\"\"`, and add a piece in each round with `+=`:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(dashes(5));\n        System.out.println(\"[\" + dashes(3) + \"]\");\n        System.out.println(\"[\" + dashes(0) + \"]\");\n    }\n\n    public static String dashes(int count) {\n        String line = \"\";\n        for (int i = 0; i < count; i++) {\n            line += \"-\";\n        }\n        return line;\n    }\n}\n```\n\n```output\n-----\n[---]\n[]\n```\n\nReturning the text, instead of printing it, means the caller can put it anywhere: on a line of its own, between brackets, or in front of other text on the same line. The next step uses exactly that to draw shapes.\n\n(Java has a ready-made way to do this, `\"-\".repeat(5)`. Here the point is the loop, so the challenges ask you to write it yourself.)\n","task":"Write a method `repeat(String text, int times)` that returns `text` repeated `times` times. The check runs `System.out.println(repeat(\"ab\", 3));` and expects:\n\n```\nababab\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(repeat(\"ab\", 3));\n    }\n\n    // Write the repeat method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(repeat(\"ab\", 3));\n    }\n\n    public static String repeat(String text, int times) {\n        String result = \"\";\n        for (int i = 0; i < times; i++) {\n            result += text;\n        }\n        return result;\n    }\n}\n","hints":["Like `dashes`, with `text` in place of `\"-\"` and `times` in place of `count`.","Start with `String result = \"\";`, add `text` in each round, and return `result` after the loop."],"tests":[{"name":"System.out.println(repeat(\"ab\", 3));","stdin":"","call":"System.out.println(repeat(\"ab\", 3));\n","expect":"ababab","hidden":false},{"name":"System.out.println(repeat(\"#\", 1));","stdin":"","call":"System.out.println(repeat(\"#\", 1));\n","expect":"#","hidden":true},{"name":"System.out.println(\"[\" + repeat(\"x\", 0) + \"]\");","stdin":"","call":"System.out.println(\"[\" + repeat(\"x\", 0) + \"]\");\n","expect":"[]","hidden":true}],"require":[],"forbid":[{"pattern":"\\.repeat\\s*\\(","message":"Build the text with a loop, without Java's ready-made `.repeat`."}],"more":[{"task":"Write `printStripes(int width, int rows)`, which prints `rows` lines, each `width` characters long. The lines take turns: `=` first, then `-`, then `=` again, and so on. Use the `repeat` method that's already there. The check runs `printStripes(5, 3);` and expects:\n\n```\n=====\n-----\n=====\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printStripes(5, 3);\n    }\n\n    public static String repeat(String text, int times) {\n        String result = \"\";\n        for (int i = 0; i < times; i++) {\n            result += text;\n        }\n        return result;\n    }\n\n    // Write the printStripes method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printStripes(5, 3);\n    }\n\n    public static String repeat(String text, int times) {\n        String result = \"\";\n        for (int i = 0; i < times; i++) {\n            result += text;\n        }\n        return result;\n    }\n\n    public static void printStripes(int width, int rows) {\n        for (int row = 1; row <= rows; row++) {\n            if (row % 2 == 1) {\n                System.out.println(repeat(\"=\", width));\n            } else {\n                System.out.println(repeat(\"-\", width));\n            }\n        }\n    }\n}\n","hints":["A loop over the rows. Odd rows (1, 3, 5, ...) are `=`, even rows are `-`: `row % 2 == 1` tells them apart.","Each row is one line: `System.out.println(repeat(\"=\", width));`"],"tests":[{"name":"printStripes(5, 3);","stdin":"","call":"printStripes(5, 3);\n","expect":"=====\n-----\n=====","hidden":false},{"name":"printStripes(2, 4);","stdin":"","call":"printStripes(2, 4);\n","expect":"==\n--\n==\n--","hidden":true},{"name":"printStripes(7, 1);","stdin":"","call":"printStripes(7, 1);\n","expect":"=======","hidden":true}],"require":[{"pattern":"\\brepeat\\s*\\(","min":2,"message":"Build each line with `repeat`."}],"forbid":[]},{"task":"Write `printFrame(int width, int height)`, which prints a frame of `#`: the top and bottom lines are full, and the lines between have a `#` at each end with spaces between. Width and height are always at least 2. Use `repeat`. The check runs `printFrame(5, 4);` and expects:\n\n```\n#####\n#   #\n#   #\n#####\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printFrame(5, 4);\n    }\n\n    public static String repeat(String text, int times) {\n        String result = \"\";\n        for (int i = 0; i < times; i++) {\n            result += text;\n        }\n        return result;\n    }\n\n    // Write the printFrame method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printFrame(5, 4);\n    }\n\n    public static String repeat(String text, int times) {\n        String result = \"\";\n        for (int i = 0; i < times; i++) {\n            result += text;\n        }\n        return result;\n    }\n\n    public static void printFrame(int width, int height) {\n        System.out.println(repeat(\"#\", width));\n        for (int row = 2; row < height; row++) {\n            System.out.println(\"#\" + repeat(\" \", width - 2) + \"#\");\n        }\n        System.out.println(repeat(\"#\", width));\n    }\n}\n","hints":["The top and bottom lines are `repeat(\"#\", width)`. The lines between them number `height - 2`.","A middle line is `\"#\" + repeat(\" \", width - 2) + \"#\"`."],"tests":[{"name":"printFrame(5, 4);","stdin":"","call":"printFrame(5, 4);\n","expect":"#####\n#   #\n#   #\n#####","hidden":false},{"name":"printFrame(2, 2);","stdin":"","call":"printFrame(2, 2);\n","expect":"##\n##","hidden":true},{"name":"printFrame(6, 3);","stdin":"","call":"printFrame(6, 3);\n","expect":"######\n#    #\n######","hidden":true}],"require":[{"pattern":"\\brepeat\\s*\\(","min":2,"message":"Build the lines with `repeat`."}],"forbid":[]}]}