M2 L notes - CSC 211 NOTES W i nt e r 2 0 1 1 A mb e r S e...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 211 NOTES Winter 2011: Amber Settle Module: Java I/O Input and output (I/O) We have seen how to cause our programs to display information, that is, output information. We have not yet seen how to input information into our programs, which is what we will now cover. We will discuss two approaches to input: 1. Using the built-in class Scanner (which is a Java 1.5+ feature). 2. Using the built-in class JOptionPane (available in older releases) Input from the console window: Scanner The Scanner class allows a program to read input taken from standard input. This corresponds to the console window in which standard output (information sent to System.out.println or System.out.print ) also appears. It can be found in the java.util package so you must put import java.util.*; in your program in order to use it. The import statement goes at the top of your program below the header comments but above the declaration of the class. Numerical input To use the Scanner class to input numeric values, you need to perform the following steps: 1. Declare and initialize a Scanner object This is done with a statement of the form: Scanner nameOfScanner = new Scanner(System.in); where nameOfScanner is any valid identifier and System.in is the name of the standard window used for input 2. Use the Scanner object to obtain an integer value This is done with a statement of the form: varName = nameOfScanner.nextInt(); where varName is a variable declared to be of type int and nameOfScanner is declared and initialized as a Scanner object.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 3. Use the Scanner object to obtain a decimal (double) value This is done with a statement of the form: varName = nameOfScanner.nextDouble();
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 4

M2 L notes - CSC 211 NOTES W i nt e r 2 0 1 1 A mb e r S e...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online