top of page
Search
  • Writer's pictureHarshit Sharma

HOW TO TAKE USER INPUT

THE INPUT FROM USER IS TAKEN BY THREE METHODS:

USING KEYBOARD CLASS:

One can create a class, which would contain methods to cater for input of the various data types. Another option is to search the internet for the Keyboard Class. This class is easily found as it is used in beginners Java courses. This class is usually found in compiled version, i.e. keyboard.class. This file has to be put in the project folder or else placed directly in the Java JDK. The following is the source code for the Keyboard class just in case it is not available online! import java.io.*; import java.util.*; public class Keyboard

USING SCANNER CLASS:

In Java 5 a particular class was added, the Scanner class. This class allows users to create an instance of this class and use its methods to perform input. Let us look at the following example which performs the same operation as the one above (works out the average of three numbers): import java.util.Scanner;

Using Swing Components:

This is probably the most exciting version, since the Swing package offers a graphical user interface (GUI) which allows the user to perform input into a program via the mouse, keyboard and other input devices. import javax.swing.*; // * means „all‟

1 view0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page