java tutorials and useful tips for developer.

Friday, 10 April 2015

How to get values from user in java

In this tutorial i will show you how to code a program in java that takes values from user and then print on console,so lets start.
These are the steps that you follow:
1) I assumed you are able to create a java project in eclipse,after creating the project you see your project on left bar of your eclipse interface,now if you click on the project you will see the folder with name "Source Packages" if you click on this folder it will show you a package with name "Default package",now right click on that and create a new java class with any name,In my case i am going to make a class with name HellowWorld.

2) After creating the class you have to write the following code.For example

             public class HelloWorld{

                     String userinput;

                  public   HellowWorld(){
                         
                           
                             

}
                      //Now we make getter setter for this variable

              public void setUseinput(String up){

                                     userinput=up;
}

public String getUserinput(){

               return userinput;

}

public static void main(String arg[]){

       HellowWorld obj=new HellowWorld();

                     Scanner input=new Scanner(System.in);//this is used for get values from keyboard.

       System.out.println("Enter the String");
       int var=input.nextInt();

     obj.setUserinput(var);
           System.out.println("User entered string"+obj.getUserinput);

}

}

So when you run this program it will take input from keyboard and print on the console.

0 comments:

Post a Comment

Categories

Popular Posts

Live Stats

Powered by Blogger.