Sunday, 19 August 2012

Java Input Functions


Introduction of Java Input (readLine())


To take input in java we use readLine() function. This function takes input inform of string.

Process to take input in java
  1. Include console class in file

    import java.io.Console;
  2. Use System console
    Console con=System.console();
  3. Use readLine() method to take input
    String str=con.readLine('message');
    
Save this file Test.java

class CRectangle
 {
 	private int length;
 	private int breadth;
 	
 	CRectangle()
 	{
 		System.out.println("Class CRectangle : Default constructor called...");
 		length = 5;
 		breadth = 3;
 	}
 	
 	CRectangle(int l, int b)
 	{
 		System.out.println("Class CRectangle : Parametrized constructor called..");
 		length = l;
 		breadth = b;
 	}
 	
 	void setLength(int l){length = l;}
 	int getLength(){return length;}
 	
 	void setBreadth(int b){breadth = b;}
 	int getBreadth(){return breadth;} 
}
import java.io.*;
import java.io.console;
class Test
{
	public static void main(String args[])
	{
	    int length,breadth;
	    Console con=System.console();
	    length=Integer.parseInt(con.readLine('length enter'));
	    breadth=Integer.parseInt(con.readLine('breadth enter'));
		CRectangle r;
		r = new CRectangle(length,breadth);
		System.out.println("Length  : " + r.getLength());
		System.out.println("Breadth : " + r.getBreadth());
	}
}

No comments:

Post a Comment