Console Input

This page is under construction. Please come back later.
/******************************************************************************
* This program demonstrates how to prompt the user for input.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
import org.pureprogrammer.Utils;
public class ConsoleInput1 {
public static void main(String[] args) {
final String NAME = Utils.prompt("What is your name? ");
final String FAVORITE_COLOR = Utils.prompt("What is your favorite color? ");
System.out.println(Utils.format("Hello, {0:s}! I like {1:s} too!", NAME, FAVORITE_COLOR));
}
}
Output
$ java -ea ConsoleInput1 < ../../examples/ConsoleInput1.in
What is your name? Rich
What is your favorite color? blue
Hello, Rich! I like blue too!
/******************************************************************************
* This program demonstrates how to prompt the user for input.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
import org.pureprogrammer.Utils;
public class ConsoleInput2 {
public static void main(String[] args) {
int favoriteInt = 0;
long favoriteLong = 0;
double favoriteDouble = 0.0;
try {
final String FAVORITE_INT_INPUT = Utils.prompt("What is your favorite small integer? ");
favoriteInt = Integer.parseInt(FAVORITE_INT_INPUT);
final String FAVORITE_LONG_INPUT = Utils.prompt("What is your favorite large integer? ");
favoriteLong = Long.parseLong(FAVORITE_LONG_INPUT);
final String FAVORITE_DOUBLE_INPUT = Utils.prompt("What is your favorite floating point? ");
favoriteDouble = Double.parseDouble(FAVORITE_DOUBLE_INPUT);
} catch (NumberFormatException ex) {
System.out.println(Utils.join("", "Bad input! ", Utils.exceptionMessage(ex)));
} catch (Exception ex) {
System.out.println("Don't know what went wrong!");
}
final double SUM = favoriteInt + favoriteLong + favoriteDouble;
System.out.println(Utils.format("All together they add up to {0:f}!", SUM));
}
}
Output
$ java -ea ConsoleInput2 < ../../examples/ConsoleInput2.in1
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? 3.141926
All together they add up to 1000329.141593!
This always prints!
$ java -ea ConsoleInput2 < ../../examples/ConsoleInput2.in2
What is your favorite small integer? 326
What is your favorite large integer? abc
What is your favorite floating point? 3.141926
Bad input!
This always prints!
$ java -ea ConsoleInput2 < ../../examples/ConsoleInput2.in3
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? abc
Bad input!
This always prints!
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Java Language Specification]], Java SE 17 Edition, Gosling, et. al., 2021.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


