- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathStringtointeger.java
More file actions
Latest commit
35 lines (30 loc) · 974 Bytes
/
Copy pathStringtointeger.java
File metadata and controls
35 lines (30 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importjava.io.*;
publicclassStringtointeger {
// Function to convert String to integer
publicstaticintconvert(Stringstr)
{
intval = 0;
System.out.println("String = " + str);
// Convert the String
try {
val = Integer.parseInt(str);
}
catch (NumberFormatExceptione) {
// This is thrown when the String
// contains characters other than digits
System.out.println("Invalid String");
}
returnval;
}
// Driver code
publicstaticvoidmain(String[] args)
{
Stringstr = "1234";
intval = convert(str);
System.out.println("Integer value = " + val);
System.out.println();
str = "123s";
val = convert(str);
System.out.println("Integer value = " + val);
}
}