0% found this document useful (0 votes)
11 views12 pages

Java Command Line and Console Input Guide

The document explains how to handle command-line arguments and console input in Java. It details the use of the Scanner and BufferedReader classes for reading input, emphasizing that command-line arguments are passed as strings and must be converted to their appropriate types manually. Additionally, it describes the process of wrapping InputStream with InputStreamReader and BufferedReader to facilitate reading higher-level data types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Java Command Line and Console Input Guide

The document explains how to handle command-line arguments and console input in Java. It details the use of the Scanner and BufferedReader classes for reading input, emphasizing that command-line arguments are passed as strings and must be converted to their appropriate types manually. Additionally, it describes the process of wrapping InputStream with InputStreamReader and BufferedReader to facilitate reading higher-level data types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Input

 Command Line Arguments


 Console Input
◦ Scanner Class
◦ Bufferedreader Class
 A command-line argument is the information
that directly follows the program’s name on
the command line when it is executed.
 To access the command-line arguments
inside a Java program is quite easy—they are
stored as strings in the String array passed to
main( ).
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<[Link]; i++)
[Link]("args[" + i + "]: " +args[i]);
}
}
 Try executing this program, as shown here:
java CommandLine this is a test 100 -1
 All command-line arguments are passed as
strings. You must convert numeric values to
their internal forms manually
 To convert string to integers:

int a=[Link](args[0]);
 Scanner Class
 BufferedReader Class
 It is a member of [Link] package
 Types:
◦ next() or nextLine()
◦ nextInt()
◦ nextFloat()
◦ nextDouble()
◦ nextLong()
Import [Link].*;
class ex{
public static void main(String ar[]){
Scanner sc=neew Scanner([Link]);
[Link](“enter name:”);
String name=[Link]();
[Link](“enter age:”);
int age=[Link]();
}}
 It is a member of [Link] package
 Helps in reading input from keyboard
Keyboard Input

The System class in java provides an InputStream object: [Link]


And a buffered PrintStream object [Link]

The PrintStream class ([Link]) provides support for outputting


primitive data type values.

However, the InputStream class only provides methods for reading byte
values.

To extract data that is at a “higher level” than the byte, we must “encase”
the InputStream, [Link], inside an InputStreamReader object that
converts byte data into 16-bit character values (returned as an int).

We next “wrap” a BufferedReader object around the InputStreamReader to


enable us to use the methods read( ) which returns a char value and
readLine( ), which return a String.
Keyboard Input

BufferedReader
InputStreamReader

byte int string

import [Link].*; //for keyboard input stream

InputStreamReader isr = new InputStreamReader([Link]);


BufferedReader br = new BufferedReader(isr);
String st = [Link]( );
//reads chars until eol and forms
string
 Input is always in form of String and can be
converted using [Link]() method

You might also like