Java: Greet a visitor

Read two lines of text and print a greeting built from them.

What to build

A program that greets someone and counts the letters of their name.

Requirements

  • Read the name (line 1) and the city (line 2) with a Scanner.
  • Print Hello, NAME from CITY!, then Your name has N characters.

Input

Line 1: a name. Line 2: a city.

Output

Two lines.

Constraints

The name and the city are 1 to 40 characters long.

Example

Input:

Ada
London

Output:

Hello, Ada from London!
Your name has 3 characters.

Why it works

nextLine() reads a whole line, spaces included, so names with spaces work, and length() counts every character of the String.

Examples

Example 1 · Input
Ada
London
Expected output
Hello, Ada from London!
Your name has 3 characters.
Example 2 · Input
Rahim Uddin
Dhaka
Expected output
Hello, Rahim Uddin from Dhaka!
Your name has 11 characters.

Hints

Hint 1

Read both lines with nextLine().

Hint 2

A String's length() gives its number of characters.

Solution

Show one correct solution (try it yourself first)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        String city = in.nextLine();
        System.out.println("Hello, " + name + " from " + city + "!");
        System.out.println("Your name has " + name.length() + " characters.");
    }
}

Lesson: Your first Java program →