Translate

Monday 18 August 2014

Reading an input and Creating object of a class

Object:-
Lets assume there is a class Matrix.
To create its object we will simply write
Matrix m = new Matrix();
OR
Matrix m;
m = new Matrix();

Reading an input:-
Here we will create object of console class to read a number, For this we will have to import java.io.*;

Console con = System.console();
String str;
str = con.readline();

Sample code:-
import java.io.*;
class ReadData
{
    public static void main(String args[])
    {
        Console con = System.console();
        String name, city;
        System.out.println("Enter your name");
        name = con.readLine();
        System.out.println("Where do you live?");
        city = con.readLine();
        System.out.println("Hello "+name+" from "+city+" You are welcome" );
    }
}

No comments:

Post a Comment

Total Pageviews