private static void output(@Nonnull final String format, @Nonnull final Object... args)
{
System.out.println(format(format, args));
}
public static void main(final String[] args)
{
final Scanner sis = new Scanner(System.in);
output(HELP_MESSAGE);
while (sis.hasNext())
{
if (sis.hasNextInt())
{
final int next = sis.nextInt();
output("You entered an Integer = %d", next);
}
else if (sis.hasNextLong())
{
final long next = sis.nextLong();
output("You entered a Long = %d", next);
}
else if (sis.hasNextDouble())
{
final double next = sis.nextDouble();
output("You entered a Double = %f", next);
}
else if (sis.hasNext("\\d+"))
{
final BigInteger next = sis.nextBigInteger();
output("You entered a BigInteger = %s", next);
}
else if (sis.hasNextBoolean())
{
final boolean next = sis.nextBoolean();
output("You entered a Boolean representation = %s", next);
}
else if (sis.hasNext(DATE_PATTERN))
{
final String next = sis.next(DATE_PATTERN);
output("You entered a Date representation = %s", next);
}
else // unclassified {
final String next = sis.next();
if (isValidURL(next))
{
output("You entered a valid URL = %s", next);
}
else {
if (EXIT_COMMANDS.contains(next))
{
output("Exit command %s issued, exiting!", next);
break;
}
else if (HELP_COMMANDS.contains(next)) { output(HELP_MESSAGE); }
else { output("You entered an unclassified String = %s", next); }
}
}
}
/* Bu asosiy o'qiladigan, bu holda System.in ni yopadi va bu resurslarni bo'shatadi.
.close() ga qo'ng'iroq qilganingizdan keyin siz System.in'dan boshqa o'qiy olmaysiz.
Agar siz System.in-dan boshqa narsa uchun foydalanmoqchi bo'lsangiz, Skanerni yopmang
*/ sis.close();
System.exit(0);
}
}
Maxsus ajratuvchilardan foydalanish
Skaner yordamida .useDelimiter(",") yordamida maxsus ajratgichlardan (muntazam ifodalardan) foydalanishingiz mumkin.
kiritish o'qiladi. Bu String.split(...) ga o'xshash ishlaydi. Masalan, roʻyxatni oʻqish uchun Skanerdan foydalanishingiz mumkin
Stringdagi vergul bilan ajratilgan qiymatlar: