What is Input from keyboard in java ?

input from keyboard

Input from the keyboard:-

Followings are the techniques through which we can take input from keyboard.

  • By using scanner class.
  • By using command line argument.
  • By using buffer-reader.
  • By using console class.
  • By using data input stream reader.

By using command line argument

Command-line Argument Command line argument is a method which user will give inputs through the console(command prompt) using commands.

class Test

{

public static void main(String[] args)

{

System.out.println(Integer.parseInt(args[0]));

}

}


By using the scanner class

Scanner class is used to input the primitive data types such as integer, double etc.

class Test

{

public static void main(String[] args)

{

   java.util.Scanner sc=new java.util.Scanner(System.in);

   System.out.println("enter your name");
   String name=sc.next();
   System.out.println("enter your roll");
   int roll=sc.nextInt();
   System.out.println("enter your cgpa");
   float cgpa=sc.nextFloat();
   System.out.println("enter your fees");
   long fees=sc.nextLong();
   System.out.println("--------------------");

  System.out.println("name:"+name);

  System.out.println("roll:"+roll);

  System.out.println("cgpa:"+cgpa);

  System.out.println("fees:"+fees);

}

}

By using Buffered Reader class

It reads text from a character-input stream.

class Test

{

public static void main(String[] args) throws java.io.IOException

{

java.io.InputStreamReader isr=new java.io.InputStreamReader(System.in);

//need inputstream present in system.in class

java.io.BufferedReader br=new java.io.BufferedReader(isr);

//need object of inputstreamreader

System.out.println("enter your name");

String name=br.readLine();//br object of bufferreader //readLIne input from keyboard
System.out.println("enter your roll");

int roll=Integer.parseInt(br.readLine());
System.out.println("enter your cgpa");

double cgpa=Double.parseDouble(br.readLine());
System.out.println("enter your fees");

long fees=Long.parseLong(br.readLine());
System.out.println("--------------------");

System.out.println("name:"+name);

System.out.println("roll:"+roll);

System.out.println("cgpa:"+cgpa);

System.out.println("fees:"+fees);

}

}

By using console class

Java Console is a simple debugging aid, that redirects any System.out and System.err to the console window

import java.io.Console;

class Test

{

public static void main(String[] args)

{

java.io.Console c=System.console();

System.out.println("enter the name");

String name=c.readLine();

System.out.println("enter the roll");

int roll=Integer.parseInt(c.readLine());

System.out.println("name:"+name);

System.out.println("roll:"+roll);

}

}

Input from DatainputStreamReader

In Java DatainputStreamReader allows an application to read primitive data from the input stream.

Java application generally uses the data output stream, to write data that can be read by a data input stream.

import java.io.DataInputStream;

public class datainputstreamreader
{
public static void main(String[] args) throws java.io.IOException
{
java.io.DataInputStream dis=new java.io.DataInputStream(System.in);
System.out.println("enter your choice");
char ch=(char)dis.read();

if(ch=='Y' || ch=='y')
System.out.println("success");
else 
if(ch=='N' || ch=='n')
System.out.println("not required");
else
System.out.println("entr a valid choice");
}
}

 

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

Leave a Reply

shares