Thursday, February 12, 2015

System Input Output

Lets Go through System input and output using scanner i.e. take input from the user and display output depending on the input.

- First thing we need to import the scanner package
- Create a scanner object.
- store the value entered by the user in a variable and then you can display or modify the value.
Below is the simple example which explains the same.

package Example1;
import java.util.Scanner;//scanner package

public class hasNextInt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanobj=new Scanner(System.in);//create the object
  System.out.println("Enter the value");
   int result=scanobj.nextInt();//store the user entered value in a variable
   System.out.println("You have entered "+result); //display the value
  
}

Output:
Enter the value 10
You have entered 10

If you follow the comments it is self explanatory.Instead of nextInt(), you can also use nextFloat(),nextDouble() etc
>>>hasNext()
------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment