Использование шаблонов регулярных выражений и встроенный Pattern класс:
import java.util.Scanner;
import java.util.regex.Pattern;
public class TestClass {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
boolean containsDigit = Pattern.compile("[0-9]").matcher(input).find();
boolean containsNonDigitNonPeriod =
Pattern.compile("[!--/:-~]").matcher(input).find();
int numberOfPeriods = input.replaceAll("[^.]", "").length();
if (containsDigit && !containsNonDigitNonPeriod)
{
if (numberOfPeriods > 1)
System.out.println("A string has been input.");
else if (numberOfPeriods == 1)
System.out.println("A float has been input.");
else
System.out.println("An integer has been input.");
}
else
System.out.println("A string has been input.");
scanner.close();
}
}
В чем вопрос? – David
http://stackoverflow.com/questions/5333110/checking-input-type-how –
Вам необходимо выполнить следующие правила: | – Javant