Привет!
Решала сама, собирая информацию из разных источников. Решение прошло валидатор и в целом программа работает.
Но я не очень понимаю, оптимально ли мое решение? Или лучше так не делать?
Буду благодарна за комментарии и советы по оптимизации.
Спасибо!
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Pattern ptDouble = Pattern.compile("(\\d+\\.\\d+|-\\d+\\.\\d+)");
Pattern ptNumbers = Pattern.compile("(\\d+|-\\d+)");
while (true) {
String str = reader.readLine();
if (str.equals("exit")) break;
Matcher mDouble = ptDouble.matcher(str);
Matcher mShort = ptNumbers.matcher(str);
if (mDouble.find()) {
String doubleValue = mDouble.group();
Double value = Double.parseDouble(doubleValue);
print(value);
} else if (mShort.find() && !mDouble.find()) {
String numbersValue = mShort.group();
Integer intValue = Integer.parseInt(numbersValue);
if (intValue > 0 && intValue < 128) {
String value = Integer.toString(intValue);
Short valueS = Short.parseShort(value);
print(valueS);
} else if (intValue <= 0 || intValue >= 128) {
print(intValue);
}
} else {
print(str);
}
}
}
public static void print(Double value) {
System.out.println("Это тип Double, значение " + value);
}
public static void print(String value) {
System.out.println("Это тип String, значение " + value);
}
public static void print(short value) {
System.out.println("Это тип short, значение " + value);
}
public static void print(Integer value) {
System.out.println("Это тип Integer, значение " + value);
}
}