Java Tips and Tricks

Interacting With the User Through Java by Kelly Lynn

  While there is plenty you can do with Java through non-interactive code, sooner or later you will encounter a problem that needs user input. There are three basic ways to do this. 1. Direct Typing This is the simplest way. It allows the user to input variables of classes such as boolean, string, long, int, and double without creating an input box. In Eclipse, the text is typed into the console screen. Here’s an example: import java.util.Scanner; public class InputTest { public static void main(String[] args){ Scanner inputScanner = new Scanner(System. in ); System. out .print( "What is your name? " ); String name = inputScanner.next(); System. out .print( "What year is it? " ); int year = inputScanner.nextInt(); }} The result would look like this: What is your … Continue reading