Учёл все варианты чисел, отрицательные с плавающей точкой в том числе, всё равно не работает, а валидатор вообще ругается на проблемы, которых нет.
Если что метод toInt возвращает массив из 2х чисел, первое число - 1 или 0 (1 если число получилось преобразовать, 0 если нет), а второе - либо 0, либо преобразованное число
package com.javarush.task.task15.task1519;
import java.io.IOException;
import java.util.Scanner;
/*
Разные методы для разных типов
*/
public class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
while (!str.equals("exit")) {
int[] ints = toInt(str);
if (count(str, '.') == 1) {
print(Double.parseDouble(str));
} else if ((ints[0] == 0)) {
print(str);
} else if (ints[1] < 128 && ints[1] > 0) {
short sh = (short) Integer.parseInt(str);
print(sh);
} else {
print(ints[1]);
}
str = sc.nextLine();
}
}
public static int[] toInt(String str) {
int[] ints = new int[2];
String regex = "\\d+";
if (str.matches(regex)) {
ints[0] = 1;
ints[1] = Integer.parseInt(str);
} else if (str.charAt(0) == '-') {
StringBuilder res = new StringBuilder();
for (int i = 1; i < str.length(); i++) {
res.append(str.charAt(i));
}
if (res.toString().matches(regex)) {
ints[0] = 1;
ints[1] = -(Integer.parseInt(res.toString()));
return ints;
}
}
return ints;
}
public static int count(String str, char ch) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch)
count++;
}
return count;
}
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);
}
}