-
Notifications
You must be signed in to change notification settings - Fork 35
/
Stringtointeger.java
35 lines (30 loc) · 974 Bytes
/
Stringtointeger.java
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
import java.io.*;
public class Stringtointeger {
// Function to convert String to integer
public static int convert(String str)
{
int val = 0;
System.out.println("String = " + str);
// Convert the String
try {
val = Integer.parseInt(str);
}
catch (NumberFormatException e) {
// This is thrown when the String
// contains characters other than digits
System.out.println("Invalid String");
}
return val;
}
// Driver code
public static void main(String[] args)
{
String str = "1234";
int val = convert(str);
System.out.println("Integer value = " + val);
System.out.println();
str = "123s";
val = convert(str);
System.out.println("Integer value = " + val);
}
}