Create a more advanced Hello World program in which you could insert your own name. So, the program can ask for an input. In this case, the program will ask for your name and give after that the output: “Hello World” and the name of the programmer.

package HelloWorld;

import java.io.PrintStream;

class HelloWorld {
	PrintStream out;

	HelloWorld() {
		PrintStream out = new PrintStream(System.out);
	}

	void start() {
		out.printf("Hello World");
	}

	public static void main(String[] args) {
		new HelloWorld().start();
	}
}

For this extra option, you have to add a scan option. In the previous part (Hello World) the PrintStream we used “System.out”, now we will use “System.in”. the whole line we have to add is “ScannerĀ in = new Scanner(System.in);”. But as we did before we split “Scanner in;” and “in = new Scanner(System.in);”, and of course import the scanner function “import java.util.Scanner;”. Something you also have to do is close the scanner with: “in.close();”.

Click here for more information about print (println, printf, etc..)

Click here for more information about scanner in (in.nextLine, in.nextInt, etc..)

package HelloWorld;

import java.util.Scanner;
import java.io.PrintStream;

class HelloWorldAdvanced {
	PrintStream out;
	Scanner in;
    
	HelloWorldAdvanced() {
		out = new PrintStream(System.out);
		in = new Scanner(System.in);   
	}

	void start() {
		out.printf("Enter your name: ");
		String name = in.nextLine();
		in.close();
		
		out.printf("Hello world!! ");
		out.printf("written by: %s\n", name);
	}

	public static void main(String[] args) {
		new HelloWorldAdvanced().start();
	}
}