Conversion String To int
In Project, if we read data from
webpage,it always returns data in the form of String.
String
+ 100 -->
"100" + 100 --> 100100
to perform addition --> 100 +100 --> 200
package DateBasics;
public class ConvertStringToint {
public
static void main(String[] args) {
//
Convert String to int
//Declare
String variable s= "123";
String
s ="123";
s = s+
10;
// "123" + 10
//
//
s = 12310
System.out.println("s="+s);//
s=12310
// to convert String to int
-Integer.parseInt(s);
//
parseInt() can be used to convert String data type to int data type
// "123" ----> 123 int
int
i = Integer.parseInt(s);
// "12310"
// 12310 int
// "12310" ---> 12310
int
i
= i +10;
// 12310 + 10
// 12320
// i =
12320
System.out.println("i="+
i);//12320
//
if u passs invalid data, string
i
= Integer.parseInt("Ram");
// Exception
in thread "main" java.lang.NumberFormatException: For input string:
"Ram"
// at
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
// "Ram"
--> cannot convert "Ram" to
int
System.out.println("i="+i);
//
note :
//
Most of the times, when we read the data from notepad or text file or excel
files or
//properties
files or
//
from web page, we get the data in the form of 'String' variable
}
}
Conversion String to Primtive Data type: int, float, char
package DateBasics;
public class ConversionsToFoat {
public
static void main(String[] args) {
// int
i=
Integer.parstInt("123");
// float
f =
Float.parseFloat("10.85");
// double
--> Double
// char --->
Character
//
Float
//
convert String to Float
//
declare String s = "2.5f";
//
parseFloat(String)
String
s = "2.5f";
float
f = Float.parseFloat(s);
// "2.5f"
// 2.5
// "2.5f" --> 2.5 float val
System.out.println("f="+
f); // 2.5
//
HW String to double
// "10.23" --> 10.23
// HW
String to boolean
// "true" --> true - Boolean values never enclosed in
""
//
Convert String to character
// "A" --> 'A'
// Character.parse --
Note: we dont have parseChar(); in Characters class
// Character.parseChar();
//
HW convert String to long
//FAQ
: How can you convert String to int
}
}
//FAQ : how can we convert int
to String ?
Conversion int To String :
Int à
String
float --> String
long ---> String
char
--> String
boolean
--> String
String s = String.valueOf(i);
package DateBasics;
public class ConvertIntToString {
public
static void main(String[] args) {
// String.valueOf(i);
//
can be used to convert int to String value
//
Declare i =10;
int
i =10 ;
//
Convert int to string String.valueOf
String
s = String.valueOf(i);
// 10 --->
"10"
// "10"
// s = "10"
System.out.println("s="+
s); // 10 --> "10"
//
convert any primitve data type to string
// float --> String
//
long ---> String
//
char --> String
//
boolean to --> String
//convert
float val to String
//
declare f = 2.5f;
float
f = 2.5f;
String s2 = String.valueOf(f);
// 2.5f
// "2.5"
// 2.5f --->
"2.5"
System.out.println("s2="
+ s2);//2.5
//
HW convert long val to String
//
HW convert char val to String
//HW convert boolean val to String
}
}
ex: Deal Price: $26.95
No comments:
Post a Comment