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

2
42
java tutorial

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{        
3 
4       public static String name = "Arslan";
5       
6       // main method
7       public static void main(String[] args){
8           
9           //print variable using print
10          System.out.print(name);
11
12          //print variable using println
13          System.out.println(name);
14          
15          //print variable using print
16          System.out.println("Hello, I'm "+name);         
17          
18          //both of the following print
19          //statements printed on single line
20          
21          System.out.print("Hello,");
22          System.out.print(" I am "+name);
23
24          //both of the following print
25          //statements printed on different line
26      
27          System.out.println("Hello,");
28          System.out.println(" I am "+name);
29      
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; 
3 
4   //initialization of class
5   public class Main{        
6 
7       //declare variable
8       public static String name;
9       
10      // main method
11      public static void main(String[] args){
12          
13          System.out.println("Enter your name: ");
14          //create Scanner object 
15          Scanner sc = new Scanner(System.in); 
16          
17          //get string (text) value 
18          //from user in variable name
19          name = sc.nextLine();
20          
21          //print variable using print
22          System.out.print(name);
23
24          //print variable using println
25          System.out.println(name);
26          
27          //print variable using print
28          System.out.println("Hello, I'm "+name);         
29          
30          //both of the following print
31          //statements printed on single line
32          
33          System.out.print("Hello,");
34          System.out.print(" I am "+name);
35
36          //both of the following print
37          //statements printed on different line
38      
39          System.out.println("Hello,");
40          System.out.println(" I am "+name);
41      
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?

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here