JAVA Input-Output
Mr. Prashant Kulkarni 1
Basic Input / Output Statements:
There are many ways to take an input for the program.
Like,
console,
Graphical User Interface,
disk files,
network connection.
Output is any information that the program must convey to the user.
Like,
screen,
printer,
devices like speaker,
disk file,
network connection
Mr. Prashant Kulkarni 2
Basic Input / Output Statements:
Ways to accept input from user.
Through Console using I/O statements,
using command line arguments.
Using GUI Components.
Through Console:
Using Scanner class (Java Utility)
Using Java IO classes.
Mr. Prashant Kulkarni 3
Scanner class: -
The Scanner class can be used to read input from console.
First we need to create object of Scanner class which attached with
Standard Input Stream (System. in).
Scanner in = new Scanner (System. in);
Scanner class is defined in java.util package so we need to import it.
Methods of Scanner class:
next () : It reads single word without space and return it.
nextLine (): It reads one complete Line, i.e. String with
multiple words.
nextInt (): It is used to read an integer number.
nextDouble (): It is used to read a double type value.
nextFloat(): It reads a float type value.
nextLong(): It reads a long type value.
nextBoolean(): It reads a Boolean type value.
Mr. Prashant Kulkarni 4
e.g.
import java.util.*;
class ScannerDemo
{
public static void main (String [] arg)
{
int a, b;
Scanner in = new Scanner (System.in);
System.out.println ("Enter the value of a : ");
a = in.nextInt ();
System.out.println ("Enter the value of b : ");
b = in.nextInt ();
System.out.println ("The Value of a and b Before Swap is");
System.out.println (a + " " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println ("The Value of a and b After Swap is");
System.out.println (a + " " + b);
}
} Mr. Prashant Kulkarni 5
Command Line Arguments: -
In Java the main method has one argument String args []
i.e. array if String type.
It reads the input from command line at the time of execution of
program.
The accepted input stored in an array args [] of String type.
We can pass the arguments from the command prompt as follows…
1. Compile the program as follows.
C:\JAVA>javac CmdLineDemo.java
2. Execute the program with command line arguments as follows.
C:\JAVA>java CmdLineDemo 10 30
Mr. Prashant Kulkarni 6
e.g.
class CmdLineDemo
{
public static void main (String [] arg)
{
int a, b;
a = Integer.parseInt(arg[0]);
b = Integer.parseInt(arg[1]);
System.out.println ("The Value of a and b Before Swap is");
System.out.println (a + " " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println ("The Value of a and b After Swap is");
System.out.println (a + " " + b);
}
}
Mr. Prashant Kulkarni 7
Formatting Output: -
For printing the output to the console we have to use
System.out.println (“Some Text”);
Where,
System.out is the standard output stream i.e. console or screen.
println () is the method of output stream which displays the text on to the
screen.
Similar to println (), print () method is also there:
The print () method displays the text on previous line.
The println () method displays the text on new line.
Mr. Prashant Kulkarni 8
Stream Classes: -
Java contains stream based I/O class hierarchy.
Byte Steam and Character stream.
The classes from this hierarchy used to perform Input and Output.
Mr. Prashant Kulkarni 9
Arrays and Strings:
Array is collection of fixed number of values of same type.
The length of the array is fixed when we define it.
The Array can be of Primitive Type or Non-Primitive Type.
Syntax:
[Access] [Modifier] Type array_name [size];
OR
[Access] [Modifier] Type [size] array_name;
Example: int arr[]; OR int [] arr;
Mr. Prashant Kulkarni 10
Arrays :
Array can be One-Dimensional, 2-Dimensional or Multi Dimensional.
int arr[]; // Array Declaration
arr = new int[10]; //Memory allocation to array
OR
int arr [] =new int[10];
OR
int arr[] = {10,20,30,40,50,60,70};
Accessing the elements of array:
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + " : "+ arr[i]);
Mr. Prashant Kulkarni 11
Multi dimensional Array:
int [][] arr = new int[5][5]; //2D array or matrix
int [][][] arr = new int[10][20][10]; //a 3D array
int arr[][] = { {1, 3, 5}, //2D array with size 3x3
{2, 4, 6},
{7, 8, 9} }
Accessing multi dimensional array:
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
Mr. Prashant Kulkarni 12
Similarly, we can create array of any primitive type:
char st[] = new char[10]; // character array with size 10.
float num[] = new float[15] // float number array with size 15.
etc…
Mr. Prashant Kulkarni 13
Strings:
String is sequence of characters.
In Java String is a Class, used to manipulate strings (Sequence of chars).
String class in immutable, once created can’t be changed.
e.g.
String msg = “Welcome”;
OR
char ch[]={‘w’,’e’,’l’,’c’,’o’,’m’,’e’};
String msg = new String(ch);
Or
String msg = new String(“Welcome”);
Mr. Prashant Kulkarni 14
String Methods:
String str1 = new String(“Good Morning”);
String str2;
str1.length(): It returns number of character in String object.
str2.concat(str1): It returns concatenation of String str1 & str2
str1.substring(int from, int to): It returns substring.
str1.equals(str2): It checks equality of both strings.
str1.toLowerCase()
str1.toUpperCase()
str1.charAt(int index)
Mr. Prashant Kulkarni 15
String Array:
String [] str_arr = {"Red", ”Green", ”Blue"};
String str_arr [] = new String [4];
str_arr[0]=“Red”; str_arr[1]=“Green” ; str_arr[2]=“Blue”;
Accessing elements of String array:
for (int i = 0; i < str_arr.length; i++)
{
System.out.print(str_arr[i]);
}
Mr. Prashant Kulkarni 16
StringBuffer class:
It is used to create mutable strings.
It has similar features like String class.
We can increase its size after creation.
Constructors:
StringBuffer(): Creates empty string with capacity of 16.
StringBuffer(String str): Creates string buffer with specified string.
StringBuffer(int length): Creates string buffer with specified length.
e.g.
StringBuffer str = new StringBuffer(“Good Morning”);
Mr. Prashant Kulkarni 17
StringBuffer class Methods:
StringBuffer str = new StringBuffer(“Welcome”);
str.length()
str.capacity()
str.append(“Students”) => str = WelcomeStudents
str.insert(int index, String str)
str.insert(int index, char ch)
str.reverse()
e.g.
StringBuffer s = new StringBuffer(“Welcome");
s.insert(7, “ students"); => str = Welcome Students
s.insert(8, 15); => str = Welcome 15 Students
Mr. Prashant Kulkarni 18