{"id":"reading-input-joining-strings","slug":"joining-strings","title":"Joining strings with +","text":"Between pieces of text, `+` joins them into one longer text. This is called **concatenation**. It works with variables too, so a program can build a sentence around what the user typed:\n\n```java\nScanner scanner = new Scanner(System.in);\nSystem.out.println(\"What is your name?\");\nString name = scanner.nextLine();\nSystem.out.println(\"Hi \" + name + \", nice to meet you!\");\n```\n\n```input\nAda\n```\n\n```output\nWhat is your name?\nHi Ada, nice to meet you!\n```\n\n(From here on, some examples show only the lines inside `main`. The class, the main method and the import around them are the same as before.)\n\n`+` adds no spaces of its own. Every space in the result has to be inside some quotes: `\"Hi \" + name` gives `Hi Ada`, but `\"Hi\" + name` gives `HiAda`. The same goes for commas and other punctuation.\n\nA single `println` can join as many pieces as you like: `\"a\" + b + \"c\" + d` works from left to right.\n","task":"The variable `language` holds `Java`. Print `I am learning Java!` by joining the variable between two pieces of text, instead of typing `Java` into the text.\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        String language = \"Java\";\n        // Print: I am learning Java! (use the variable)\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        String language = \"Java\";\n        // Print: I am learning Java! (use the variable)\n        System.out.println(\"I am learning \" + language + \"!\");\n    }\n}\n","hints":["The text before the variable is `\"I am learning \"`, with a space at the end.","Join three pieces: `\"I am learning \" + language + \"!\"`."],"tests":[{"name":"Output","stdin":"","expect":"I am learning Java!","hidden":false}],"require":[{"pattern":"\\+\\s*language\\s*\\+","message":"Join the variable between two pieces of text with `+`: `\"...\" + language + \"...\"`."}],"forbid":[{"pattern":"println\\s*\\([^;]*\"[^\"]*Java","raw":true,"message":"Use the variable `language` instead of typing `Java` into the printed text."}],"more":[{"task":"Ask for a name, read it, and greet the person. For the input `Ada` the program prints:\n\n```\nWhat is your name?\nHello, Ada!\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        System.out.println(\"What is your name?\");\n        String name = scanner.nextLine();\n        System.out.println(\"Hello, \" + name + \"!\");\n    }\n}\n","hints":["Print the question first, then read the name with `scanner.nextLine()`.","`\"Hello, \" + name + \"!\"` puts the name between the comma and space and the exclamation mark."],"tests":[{"name":"Test 1","stdin":"Ada\n","expect":"What is your name?\nHello, Ada!","hidden":false},{"name":"Test 2","stdin":"Linus\n","expect":"What is your name?\nHello, Linus!","hidden":true},{"name":"Test 3","stdin":"Grace Hopper\n","expect":"What is your name?\nHello, Grace Hopper!","hidden":true}],"require":[],"forbid":[]},{"task":"Read a first name and a last name, on two lines, and print them back in two ways. For the input `Ada` and `Lovelace` the program prints:\n\n```\nFirst name:\nLast name:\nNice to meet you, Ada Lovelace.\nIn a list: Lovelace, Ada\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        System.out.println(\"First name:\");\n        String first = scanner.nextLine();\n        System.out.println(\"Last name:\");\n        String last = scanner.nextLine();\n        System.out.println(\"Nice to meet you, \" + first + \" \" + last + \".\");\n        System.out.println(\"In a list: \" + last + \", \" + first);\n    }\n}\n","hints":["Two questions, two reads: each `nextLine()` reads the next line of input.","Between the names goes `\" \"`, a text that is just one space: `first + \" \" + last`.","The list form is `last + \", \" + first`."],"tests":[{"name":"Test 1","stdin":"Ada\nLovelace\n","expect":"First name:\nLast name:\nNice to meet you, Ada Lovelace.\nIn a list: Lovelace, Ada","hidden":false},{"name":"Test 2","stdin":"Alan\nTuring\n","expect":"First name:\nLast name:\nNice to meet you, Alan Turing.\nIn a list: Turing, Alan","hidden":true},{"name":"Test 3","stdin":"Margaret\nHamilton\n","expect":"First name:\nLast name:\nNice to meet you, Margaret Hamilton.\nIn a list: Hamilton, Margaret","hidden":true}],"require":[],"forbid":[]}]}