In This Example We Are Trying To Get User Inputs and Print It.
Open Text File & Save As SayHello.Create The Class and Main Method Inside It.Inside The Method Print A Text Line Asking User To Input The Name.
class SayHello {
public static void main(String args[]){
System.out.print("Enter The Name : ");
}
}
To Get The User Inputs We Need A Scanner Object.But By Default Scanner Class Is Not Available Us To Create A Scanner Object.You Need To Import The Scanner Class First.After That You Can Use The Scanner Class.Create A String Variable To Save The Name and Create A Scanner Object To Get The User Inputs.
import java.util.Scanner;
class SayHello {
public static void main(String args[]){
Scanner MyScanner = new Scanner(System.in);
String Name;
System.out.print("Enter The Name : ");
}
}
Now We Need Is Get The User Input and Print It Again In The Command Prompt.Use Scanner.in Method To Get The User Inputs.Complete The Coding.
import java.util.Scanner;
class SayHello {
public static void main(String args[]){
Scanner MyScanner = new Scanner(System.in);
String Name;
System.out.print("Enter The Name : ");
Name = MyScanner.nextLine();
System.out.print("Hello ");
System.out.print(Name);
}
}
Now Run The Program.It Will First Print "Enter The Name : " and Wait For User Input.Type The Name and Press Enter Button To See The Output.You Will Get Following Outputs At The End.
Enter The Name : Nifal
Hello Nifal
Showing posts with label print. Show all posts
Showing posts with label print. Show all posts
Jul 6, 2014
Java Variables and Printing Values
Open Text File & Save As JavaVariables.Create The Class and Main Method Inside It.
class HelloWorld {
public static void main(String args[]){
}
}
Now We Have Our JavaVariables Class. Inside The Class We Can Create Our Variables.I'm Creating Variables All Together And Printing All.
class HelloWorld {
public static void main(String args[]){
String SName = "Nifal Nizar";
int age = 24;
System.out.print("My Name Is ");
System.out.println(SName);
System.out.print("I am ");
System.out.print(age);
}
}
You Will See The Out Put As
My Name Is Nifal Nizar
I am 24
If We Are Using print Method It Will Print The Text And Wait In The Same Line.But If We Use println Method It Will Print The Text and It Will Go To Next Line.
class HelloWorld {
public static void main(String args[]){
}
}
Now We Have Our JavaVariables Class. Inside The Class We Can Create Our Variables.I'm Creating Variables All Together And Printing All.
class HelloWorld {
public static void main(String args[]){
String SName = "Nifal Nizar";
int age = 24;
System.out.print("My Name Is ");
System.out.println(SName);
System.out.print("I am ");
System.out.print(age);
}
}
You Will See The Out Put As
My Name Is Nifal Nizar
I am 24
If We Are Using print Method It Will Print The Text And Wait In The Same Line.But If We Use println Method It Will Print The Text and It Will Go To Next Line.
Subscribe to:
Posts (Atom)
JWT Token Decode Using Jquery
When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...
-
In this blog post Ill explain you the logic behind the Sri Lankan National Identity Card (NIC). Sri Lanka now has Old NIC No Format as well ...
-
In This Article I Will Demonstrate You To Create A Simple Chat Program Using Java Remote Method Invocation(RMI). Following Program Supports ...
-
In a windows form application sometimes you may want to import a excel file to a data grid view. In this blog post Ill demonstrate to you gu...