-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxDigitInString.java
51 lines (40 loc) · 1.32 KB
/
MaxDigitInString.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//**Given a String find the maximum digit within the String**
//
//**This exercise contains a class named MaxDigitInString with the following static method:**
//
// getMaxDigit(String):int
//
//- The input String to the method should be scanned for digits and the maximum digit should be returned.
//- Input String can contain alphanumeric character, spaces and special characters
//- If the input string is empty/null or it does not contain any digits, -1 should be returned.
package exercises;
import java.util.Scanner;
public class MaxDigitInString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
String userInput = scanner.nextLine().trim();
int maxDigit = MaxDigitInString.getMaxDigit(userInput);
if (maxDigit == -1) {
System.out.println("No digits in string");
} else {
System.out.println("Max digit : " + maxDigit);
}
scanner.close();
}
public static int getMaxDigit(String input) {
if (input == null || input.compareTo("")==0) {
return -1;
}
int maxDigit = -1;
for (int i=0; i<input.length(); i++) {
if (Character.isDigit(input.charAt(i))) {
int newDigit = Integer.parseInt(input.substring(i, i+1));
if (newDigit > maxDigit) {
maxDigit = newDigit;
}
}
}
return maxDigit;
}
}