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) { String name; name = Utils.prompt("What is your name? "); String favoriteColor; favoriteColor = Utils.prompt("What is your favorite color? "); System.out.println(Utils.format("Hello, {0:s}! I like {1:s} too!", name, favoriteColor)); } }
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) { try { int favoriteInt; long favoriteLong; double favoriteDouble; String favoriteIntInput; favoriteIntInput = Utils.prompt("What is your favorite small integer? "); favoriteInt = Integer.parseInt(favoriteIntInput); String favoriteLongInput; favoriteLongInput = Utils.prompt("What is your favorite large integer? "); favoriteLong = Long.parseLong(favoriteLongInput); String favoriteDoubleInput; favoriteDoubleInput = Utils.prompt("What is your favorite floating point? "); favoriteDouble = Double.parseDouble(favoriteDoubleInput); final double sum = favoriteInt + favoriteLong + favoriteDouble; System.out.println(Utils.format("All together they add up to {0:f}!", sum)); } catch (NumberFormatException ex) { System.out.println("Bad input! " + Utils.exceptionMessage(ex)); } catch (Exception ex) { System.out.println("Don't know what went wrong!"); } } }
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]]