How to get user input & print on screen in Java?

Before starting with this part, you are recommended to read the introduction of Java Programming.

How to print in Java?

There are 3 ways to print any text or value on screen in Java. These are print, println and printf. Each of them of prefixed with System.out. before use. The method print, prints the text or value on screen only but does not move the cursor to next line or new line. The method println prints the text or value but move the cursor to new line. When we have to format our text in different ways, we use printf to print. (You would like to create your first program before moving on with this part.)

When we have to print a variable value only without any text, we simply pass the variable to print or println method. If we have to print text along with variable value, we can pass value to print method enclosed in double quotes and concatenate variable with it e.g. “Hello, My name is ”+name.

1   //initialization of class
2   public class Main{        
4       public static String name = "Arslan";
6       // main method
7       public static void main(String[] args){
9           //print variable using print
10          System.out.print(name);
12          //print variable using println
13          System.out.println(name);
15          //print variable using print
16          System.out.println("Hello, I'm "+name);         
18          //both of the following print
19          //statements printed on single line
21          System.out.print("Hello,");
22          System.out.print(" I am "+name);
24          //both of the following print
25          //statements printed on different line
27          System.out.println("Hello,");
28          System.out.println(" I am "+name);
30      }       //end of main method
31   }                 //end of class

How to get input from user and print on screen?

1   //import required java packages
2   import java.util.Scanner; 
4   //initialization of class
5   public class Main{        
7       //declare variable
8       public static String name;
10      // main method
11      public static void main(String[] args){
13          System.out.println("Enter your name: ");
14          //create Scanner object 
15          Scanner sc = new Scanner(; 
17          //get string (text) value 
18          //from user in variable name
19          name = sc.nextLine();
21          //print variable using print
22          System.out.print(name);
24          //print variable using println
25          System.out.println(name);
27          //print variable using print
28          System.out.println("Hello, I'm "+name);         
30          //both of the following print
31          //statements printed on single line
33          System.out.print("Hello,");
34          System.out.print(" I am "+name);
36          //both of the following print
37          //statements printed on different line
39          System.out.println("Hello,");
40          System.out.println(" I am "+name);
42      }       //end of main method
43   }                 //end of class

At line 2, we imported java package that is required to access Scanner class to get input from user. At line 13, we prompt message to “enter your name” to user. At line 15, we initialized an object of Scanner class to access the method to get string from the user. At line 19, the user entered his name and pressed enter and the value entered by the user is stored in variable name.

To Dos.

The following activity will help you to enhance your understanding about Java basics:

Let’s make a small program to print your name on the screen. Perform the following changes on your code and see the effect of each change.

  1. What if class name is different from file name of your Java file?
  2. What if you skip public static void main (String[] args)?
  3. What if you use string[] instead of String[]?
  4. What if you use Strings[] name instead of String[] args?
  5. What if you skip static keyword?
  6. What if ( ;  ) is skipped?
  7. What if you use out.print(“ ”); instead of System.out.print(“ ”); ?
  8. What if you skip 1 or both double quotes in out.print( );
  9. What if any curly bracket is missed?

Leave a Reply

Your email address will not be published. Required fields are marked *