-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract the number from the string
51 lines (41 loc) · 1.28 KB
/
Extract the number from the string
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//Extract the number from the string
import java.io.*;
import java.util.*;
import java.util.regex.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0) {
String S = read.readLine();
Solution ob = new Solution();
System.out.println(ob.ExtractNumber(S));
}
}
}
class Solution {
long ExtractNumber(String sentence) {
long ans = 0, temp = 0;
boolean indication = false;
for(int i = 0; i < sentence.length(); i++) {
char x = sentence.charAt(i);
if(x >= '0' && x <= '9') {
if(x == '9') {
indication = true;
}
temp = temp * 10 + (x - '0');
}
else if(x == ' ') {
if(!indication) {
ans = Math.max(ans, temp);
}
temp = 0;
indication = false;
}
}
if(!indication) {
ans = Math.max(ans, temp);
}
return ans == 0 ? -1 : ans;
}
}