Sunday 7 October 2012

EVOLVING FROM C++ TO JAVA-2


TUTORIAL -2

Hi guys,this is my second tutorial in evolving from c++ to java,in my first i said about main function and file naming.In this one i will explaining you how to get AN INPUT AND TO DISPLAY IT.  

Some basic characteristics of java regarding input and output:

Java reads and write inputs as bytes.Java uses I/O streams for input and output operations. Since this is a beginners tutorial i don't want to explain fully about streams.For know you can consider stream's as a buffer ,which can store the data and connect two end points.Since java read's and write as bytes ,we must convert the bytes into different forms during reading data and convert the data that we are going to write into bytes.Java provides different streams for this conversion.For now since you are beginning in java programming i will be explaining you just how to get input and display output,as we go for further.in forth coming  tutorials i will explain you how streams work.

How to get INPUT in java?

I will explain two ways of reading a input in a java program.First i will explain you using BufferedReader.

USING BUFFEREDREADER:

 When we use bufferedreader method it reads the input as line and store it in a string.Even when we read a integer or float value it will take it as string.So when we are reading a integer or float value we must convert the string into regarding values using parse methods.
  • Step 1:Create a object to bufferreader class.Lets for example we can take that object as "in".
in=new BufferedReader(new InputStreamReader(System.in));
  • Step 2:how to use the buffereader object?
TO READ A STRING VALUE:
  • Create a string object and use readLine() or read() method.
String m;
m=in.readLine();
OR
m=in.read();
 read():It reads the string till there is a space.It wont read space,and it wont read after the space.
readLine():It reads including space,till the string we enter ends.
TO READ A INTEGER OR FLOAT OR DOUBLE VALUE
  • Create a string object and read the input using readLine() or read() method.
  • Now using parse method convert the string into your data type.


String m;
m=in.readLine();
int i=Integer.parseInt(m); //converts the string into integer value and assign it to i
Float f=Float.parseFloat(m);//converts the string into float value and assign it to f
Double d=Double.parseDouble(m);//converts the string into double value and assign it to d

Using Scanner class

I prefer to use scanner class,because its simple and easy to use.
  • Step1:Create a object for scanner class.Let the object be"in".
Scanner in=new Scanner(System.in);
  • Step2:Now just call the scanner methods to read value.
TO READ INTEGER:
int i=in.nextInt();//Reads a integer value and assign it to i

TO READ FlOAT:
Float f=in.nextFloat();//Reads a float value and assign it to f

TO READ DOUBLE:
Double d=in.nextDouble();//Reads a double value and assign it to d

TO READ STRING:
String str=in.nextLine();//Reads a string value and assign it to str.

How to display output in java?

The output method to display the result in java is similar to c++ and c.Here we use "System.out.print("") to display the output which is similar to cout and printf.
  • To add more than one data variables ,use + symbol in between them. 
  • To concatenate the data variables to a string,we can use +symbol.
System.out.println("hai");//prints the string hai in a new line.
System.out.print("hai");//prints the string hai in the same line.
System.out.println("hai"+i);//prints the string hai and variable i in a new line.
System.out.print(i+str);//prints the variable i and str in a line.It dose not perform addiction between them.

Note:

There are various method to perform input and output operations.I have just explained some basic methods to read data and display it on screen.




2 comments:

  1. for printing why do we concatenate.??

    ReplyDelete
  2. i have said to add variable to a string or too add more than one variables we must use "+" to concatenate it!!!!!!!!!!

    ReplyDelete