User Input: Programming Can Be More Productive Having Interaction From The User
User Input: Programming Can Be More Productive Having Interaction From The User
User Input: Programming Can Be More Productive Having Interaction From The User
INPUT
Programming can be
more productive
having interaction
from the user.
Getting input from the keyboard
When the program gets input from the keyboard implies that the user is
acting interactively. We have three ways to ask input.
Buffered Reader
In this section, we will use the BufferedReader class found in the java.io
package in order to get input from the keyboard. It creates a buffering
character‐input stream that uses a default‐sized input buffer.
Using bufferedReader class, first we import the bufferReader class out of
the java.io package by having this statement:
import java.io.bufferedReader;
or the other way around
import java.io.*;
Which loads all classes found in the io package to have access and use
those classes inside our program.
Then we add the statement,
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
import javax.swing.*;
public class joptionpane
{
public static void main( String[] args )
{
String name = " ";
name = JOptionPane.showInputDialog("Enter Name: ");
JOptionPane.showMessageDialog(null,"Hello " + name + "!");
}
}
Having the first statement
import javax.swing.*;
This is an indication that the class JOptionPane will be use and will be
imported from the javax.swing package.
In the next statement, it obtain users input from JOptionPane input
dialogs and stores the string in the variable name. Displayed in the
input dialog are textfield where in the user entered the data and an OK
button to submit the string to the program.
name=JOptionPane.showInputDialog("Enter first and last name: ");