PREVIOUS
Conversion
-----------------
All
inputs in java are stored in String format. Therefore, we can convert one
Type
into another using parses method.
To Convert String to int,
float, double, byte
Integer.parseInt
Float.parseFloat
Double.parseDouble
Byte.parseByte
import java.io.*;
class intro
{
int
p=10;//instance variable
static int
q=20;//class var
public static void main(String arg[])throws
IOException
{
intro i=new intro();
System.out.println("instance p = " +i.p
+"class q= " +q);
int a;
float b;
String s;
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the int,float,string
val");
a=Integer.parseInt(d.readLine());
b=Float.parseFloat(d.readLine());
s=d.readLine();
System.out.println(" int val : "+a);
System.out.println(" float val: "+b);
System.out.println(" String : "+s);
}
}
//command line arguments
import java.io.*;
class cline
{
public static void main(String ar[])throws
IOException
{
int no;
String name;
float sal;
//DataInputStream d=new
DataInputStream(System.in);
no=Integer.parseInt(ar[0]);
name=ar[1];
sal=Float.parseFloat(ar[2]);
System.out.println(+no);
System.out.println(name);
System.out.println(+sal);
}
}
compile:
javac cline.java
run:
java cline 33 anu 54.66
No comments:
Post a Comment