Skip to main content

JAVA: Reading input from Keyboard

How does JAVA accepts input from keyboard?
-streams accepts input from keyboard.
-stream is a flow of data.


Types of stream:
  • input stream : Receives the data.
  • output stream : Writes the data.
Note: 
System.out is used to print the output to the console.
System.in is used to take input from the console.


We can take input from the keyboard in two ways:
  1. using InputStreamReader class
  2. using Scanner class

PROGRAM EXAMPLE:
1. Using InputStreamReader class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class e_inputStream {
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
              // BufferedReader is used to read the data from the input stream
System.out.println("Enter the number:");
int i=Integer.parseInt(br.readLine()); //converting the text to int
System.out.println("the entered number is:"+i);
}
}

Output: 
Enter the number:
10
the entered number is:10


2. Using Scanner class
import java.io.IOException;
import java.util.Scanner;

public class f_scannerclass {
public static void main(String args[]) throws IOException{
Scanner sc=new Scanner(System.in);
//values are stored as a token in scanner object.
System.out.println("Enter the number:");
int i=sc.nextInt();  //to receive the token 
System.out.println("the entered number is:"+i);
}
}

Output:
Enter the number:
10
the entered number is:10


Note: 
print() : keeps the cursor in the same like after displaying the output.
println() : keeps the cursor in the next line after displaying the output.

Comments

Popular posts from this blog

I Wish for a WISH

Wish I can wish for a chance A chance to win A chance to be better   A chance to be the best daughter But I don’t want to regret a thing Am happy the way I think The only thing that I wish Let me be me please. I don’t want to hurt anyone I don’t want to lie I just want to see everyone happy The only thing that I wish Let me be me please.                                                                                                          ...

Fantasy Dream Poem

How nice it would be, If we can dance over the rainbow, Sleep over the clouds, Shine with the stars, And bless others with the raindrops. I know it’s a fantasy dream, But nothing is impossible!

First Love

When I first saw you, I felt my own heart beat, When I first talked to you, I felt the butterflies in me, When I first met  you , I felt like i found ma life, When I feel in love with  you , I saw my future with  you , But all those were mai thoughts, Cause  you  didn't love me at all, I thought  you  loved me, Until I saw  you  wid her, Doing whatever  you  did with me, All my feelings went away, It felt like a glass broke down, It still hurts, I hope tht  you  wud stay happy, I hope that  you would  live , All I want to see is you being happy.                                                       ...