Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures by John Lewis, Peter DePasquale and Joseph Chase

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 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

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3 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();

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4 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)Echo.java 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

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5 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)GasMileage.java

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6 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.

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7 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(",");

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8 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.

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9 Summary Today we reviewed: –Accepting input from the user –Reading input from a file –Modifying delimiters when tokenizing –GUI Dialog Boxes


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures."

Similar presentations


Ads by Google