Making Your Application Interactive

So far in the book input for our Java programs data was provided via arrays or variables that were initialized inside the code or via program arguments. But most applications nowadays require interaction with the user. The user can be provided access by e

  • PDF / 1,040,903 Bytes
  • 61 Pages / 504 x 720 pts Page_size
  • 1 Downloads / 240 Views

DOWNLOAD

REPORT


Making Your Application Interactive So far in the book input for our Java programs data was provided via arrays or variables that were initialized inside the code or via program arguments. But most applications nowadays require interaction with the user. The user can be provided access by entering a username and a password; the user is sometimes required to enter information to confirm his/her identity or to instruct the application what to do. Java supports multiple methods for user input to be read. In this chapter a few ways to build interactive Java applications are covered. Interactive Java application take their input either from the console, either from Java built interfaces, either desktop or web. JShell is a command line interface, where a developer can enter variable declarations and one line statements that are executed when the Enter key is pressed. Command line interface shells like bash and terminals like Command Prompt from Windows can issue commands to programs in the form of successive lines of text. JShell was covered at the beginning of the book for the simple reason that it was a Java 9 novelty. The next sections cover how to read user-provided data and instructions using the command-line interface. The sections after that focus on building Java applications with a desktop/web interface.

Reading Data from the Command Line This section is dedicated to reading user input from the command line, whether is the IntelliJ IDEA console, or if the program is run from an executable jar from any terminal specific to an operating system. In the JDK, there are two classes that can be used to read user data from the command line: java.util.Scanner and java.io.Console and this section cover them both in detail. Without further ado, let’s get into it.

© Iuliana Cosmina 2018 I. Cosmina, Java for Absolute Beginners, https://doi.org/10.1007/978-1-4842-3778-6_10

409

Chapter 10

Making Your Application Interactive

Reading User Data Using System.in Before introducing logging in Chapter 9 to print data in the console, methods under System.out were used. There is also a counterpart utility object named System. in used to read data from the console, data that a user of the program introduces to control the application flow. You might have noticed that until now all Java programs, when executed they would be started, they would process the data, would execute the declared statements and then they would terminate, exit gracefully or with an exception when something went wrong. The most simple and common way to pass decision of termination to the user is to end the main method with a call to System.in.read(). This method reads the next byte of data from the input stream and the program is paused until the user introduces a value, as the value is returned we can even save it and print it. import java.io.IOException; public class ReadingFormStdinDemo {     public static void main(String... args) throws IOException {         System.out.print("Press any key to terminate:");         int read = System.in.read();