Вы находитесь на странице: 1из 4

GETTING USER INPUT

• There are two basic ways (for now) to get input from the user
JAVA INPUT METHODS • The Scanner class
CSE 114: Computer Science I
Stony Brook University • JOptionPane and showInputDialog()

• Either one is okay for the purposes of this class

TRADE-OFFS

• JOptionPane is slightly easier to use, but you have to convert


the result to a usable form yourself

• Scanner is slightly more complex, but it converts the input to a


SCANNER
usable form for you

• Both options require you to use an import statement to use


utilities that are not built-in to Java
STREAM ILLUSTRATION
User input (hitting Enter after each line):
STREAMS
23

some words

• Java input and output are treated as streams of characters 114.03 more

(actually, bytes) text

to

• A stream is a sequence of data that flows in one direction process
• e.g., keyboard to program, program to display
2 3 \n s o m e w o r d s \n
• Data is processed in first-in, first-out order 1 1 4 . 0 3 m o r e \n t e
x t \n t o \n p r o c e s s \n

BASIC INPUT HOW TO USE SCANNER


• You must include
• In
Java 1.5 or later, use the Scanner class to read from the
keyboard (or other sources) import java.util.*;
• Scanner’s methods return the next value of a given type at the top of your source code file!
• nextLine(), nextInt(), nextDouble(), etc.
• To create a Scanner for use:
• These methods only work if the remaining input begins Scanner myScan = new Scanner(System.in);
with the proper type
• Creates a reference (myScan) to a Scanner object
• Most methods don’t consume newlines!
• Tells Scanner to read from the keyboard (System.in)
• Only nextLine() does this for you
SCANNER EXAMPLE OTHER SCANNER METHODS
• Scanner also provides “lookahead” methods

• hasNext(), hasNextLine(), hasNextInt(), etc.

Scanner s = new Scanner(System.in); • These methods return true if:

System.out.print(“Enter temperature: ”);
 • the input stream contains more data, AND
int temp = s.nextInt(); // read the next characters as an int

• the next value that can be read is of the given type

System.out.print(“Enter your name: ”);
 • This helps you to avoid bugs where you try to read data
String name = s.nextLine(); // read next line as a String
that’s in the wrong format (or not there at all!)

JOPTIONPANE

• Thisapproach pops up a small dialog box asking the user to


JOPTIONPANE type in some input text

• The result is a String containing whatever the user typed

• Start by telling Java where to find the JOptionPane class:




import javax.swing.*;
USING JOPTIONPANE CONVERTING THE RESULT

• To
actually display the dialog box, call
JOptionPane’s showInputDialog() • showInputDialog() always returns a String
method
• To
get this into a useful format (int, double, etc.), use a
• showInputDialog() takes a wrapper class to convert the String to the desired type
message as its argument
• e.g., Integer.parseInt() or Double.parseDouble()

• ex. Integer.parseInt(“123”) returns the int value 123


String text = JOptionPane.showInputDialog(“Enter a #:”);

NEXT TIME

• Conditionals (if statements)

• Switch statements

• Loops

Вам также может понравиться