Download Scanner Review

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Scanner Review
Java Foundations:
Introduction to Programming and Data Structures
by John Lewis,
Peter DePasquale and Joseph Chase
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Interactive Programs
• Programs generally need input on which to operate
• The Scanner class provides convenient methods for
reading input values of various types
• A Scanner object can be set up to read input from
various sources, including the user typing values on the
keyboard
• Keyboard input is represented by the System.in object
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-2
Reading Input
• The following line creates a Scanner object that
reads from the keyboard:
Scanner scan = new Scanner (System.in);
• The new operator creates the Scanner object
• Once created, the Scanner object can be used to
invoke various input methods, such as:
answer = scan.nextLine();
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-3
Reading Input
• The Scanner class is part of the java.util
class library, and must be imported into a program
to be used
• See Echo.java (p. 63 Green)
• The nextLine method reads all of the input until
the end of the line is found
• The details of object creation and class libraries
are discussed further in Chapter 3
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-4
Input Tokens
• Unless specified otherwise, white space is used to
separate the elements (called tokens) of the input
• White space includes space characters, tabs, new line
characters
• The next method of the Scanner class reads the next
input token and returns it as a string
• Methods such as nextInt and nextDouble read data
of particular types
• See GasMileage.java (p. 64)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-5
Read from File
• In the previous example, we read input from the keyboard.
• Scanner can also be used to read input from a file.
• Notice:
– Need to import java.io.*;
• See p. 145 for code sample – URLDissector
• The content of the input file, “websites.inp” is shown on p. 144.
• What separates each token input? It uses the default delimiter, white
space.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-6
Change delimiter
• Suppose we have an input file, “numbers.inp” with the
following content:
10 20 30 40 50 60 70 80 90 100
• We can change it so that the numbers are separated by
commas:
10,20,30,40,50,60,70,80,90,100
• We can change the delimiter to comma using the Scanner
method:
tokenScan.useDelimiter(",");
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-7
Scanner Input/Output Alternatives
• What if we want to receive input using a GUI dialog box instead of a text
prompt?
• What if we want to display the output uses a GUI dialog box instead of line
output?
• Take a look at p. 305-306.
• Steps to convert line output to GUI:
– Need to import javax.swing.JOptionPane;
• Input - General syntax is:
response = JOptionPane.showInputDialog(“message”);
• Output - General syntax is:
JOptionPane.showMessageDialog (null,”message”);
• In your code, change all the System.in System.out lines to JOptionPane lines.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-8
Summary
• Today we reviewed:
–
–
–
–
Accepting input from the user
Reading input from a file
Modifying delimiters when tokenizing
GUI Dialog Boxes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1-9