Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interactive Standard Input/output

Similar presentations


Presentation on theme: "Interactive Standard Input/output"— Presentation transcript:

1 Interactive Standard Input/output
In Java

2 Keyboard Input (prior to 1.5)
System.in object The connection allowing data entry from the standard input device. Referred to as the stream object. Delivers data as a stream of individual bytes. CSC 331 Object-Oriented Programming Using Java

3 Keyboard Input (prior to 1.5) Classes Methods
InputStream InputStreamReader BufferedReader read() System.in.read() Returns the key typed as an integer value. Object of this class converts from integer to a character value. readLine() br.readLine() Returns as a string the characters typed. CSC 331 Object-Oriented Programming Using Java

4 Keyboard Input (prior to 1.5)
First, construct InputStreamReader object from System.in object. InputStreamReader isr = new InputStreamReader(System.in); Next, construct a BufferedReader stream object. BufferedReader br = new BufferedReader(isr); Now, br.readLine() can be used to read a string. Example: String s1 = br.readLine(); CSC 331 Object-Oriented Programming Using Java

5 Additional Requirements for Keyboard Input (prior to 1.5)
Use appropriate import statement to access input stream classes: import java.io.*; Include throws java.io.IOException when using readline(). See KeyboardInput.java CSC 331 Object-Oriented Programming Using Java

6 The StringTokenizer class
Enables readLine() to accept multiple strings that can be converted to numeric values. Requires import java.util.*; CSC 331 Object-Oriented Programming Using Java

7 Useful Wrapper Class Conversion Routines
Integer.parseInt(string) – returns integer val. Integer.parseLong(string) – returns long val. Float.parseFloat(string) – returns float val. Double.parseDouble(string) – returns double There is a toString(arg) method for each of these, e.g., Float.toString( ) returns “ ” CSC 331 Object-Oriented Programming Using Java

8 Keyboard Input using Scanner(1.5)
First, construct Scanner object from System.in object. Scanner scannerObject = new Scanner(System.in); Next, use Scanner methods as appropriate to read integers: int n1=scannerObject.nextInt(); doubles: double d1=scannerObject.nextDouble(); and so on. Now, scannerObject.nextLine(); can be used to read a string. Example: String line = scannerObject.nextLine(); See KeyboardScanner.java. CSC 331 Object-Oriented Programming Using Java

9 Formatted Output (before 1.5)
Use the format() method from java.text.DecimalFormat Three requirements: 1. import java.text.*; 2. Include a statement that uses new to create the desired format string. 3. Use a call to format() to apply the format string to a numerical value. CSC 331 Object-Oriented Programming Using Java

10 Symbols for User-Defined Format Strings Symbol Description
# . , ; % Digit placeholder/ Digit placeholder & automatic fill character. Decimal placeholder Grouping separator. Separate positive & negative format strings. Multiply by 100 & add a % sign. CSC 331 Object-Oriented Programming Using Java

11 CSC 331 Object-Oriented Programming Using Java
Example (before 1.5): import java.text.*; public class WithFormats{ public static void main (String[ ] args){ DecimalFormat num= new DecimalFormat(“000”); System.out.println(num.format(6)); System.out.println(num.format(18)); System.out.println(num.format(124)); System.out.println(“---”); System.out.println(num.format( )); } CSC 331 Object-Oriented Programming Using Java

12 CSC 331 Object-Oriented Programming Using Java
The class Formatter (1.5) Operates similarly to C++ printf(); See Head First Java text, pp CSC 331 Object-Oriented Programming Using Java

13 CSC 331 Object-Oriented Programming Using Java
The method printf() (1.5) Operates similarly to C++ printf(); Added to the PrintStream class. Example: double price = 19.8; System.out.print(“$”); System.out.printf(“%6.2f”,price); // no new line Produces: $ 19.80 CSC 331 Object-Oriented Programming Using Java

14 GUI Interactive User Input/Output
JOptionPane.showInputDialog(string); JOptionPane.showMessageDialog( CSC 331 Object-Oriented Programming Using Java

15 CSC 331 Object-Oriented Programming Using Java
Conclusion Java is continually being updated. Many enhancements are aimed at making it easier for programmers to use. CSC 331 Object-Oriented Programming Using Java


Download ppt "Interactive Standard Input/output"

Similar presentations


Ads by Google