Sunday, April 5, 2015

Java Learning , if you know C : Input / Output : Part 6

In most of my programs in which I have to take input from console . I have used Scanner object .

The below program tells how to use it.

public class inputoutput{

public static void main(String args[]){

   System.out.println("enter any number");

 Scanner input = new Scanner(System.In);

 int x = input.nextInt();

}
}

now Scanner class provides you with lot of methods to read int, short, strings and other things .

A basic google will solve your problems.

I have not till yet tried to make any java program which reads from file. once done will blog it.


Regarding the Output .

System.out.println():  will work smoothly .

To type some explanation and then you variable

System.out.println("this is your answer" + var_name);   +  is something like ','  of C

To print multiple variables, see sample program


public class io {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a= 12;
int b=34;
String c = "leo";

System.out.println("my name and number is " + c + a +" " + b);
}

}



Unlinke C, there is typefdef format symbol needed to be put. In case you need to output Hexa value of int , there will be some method surely, I have not still explored that .


No comments:

Post a Comment