Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle."— Presentation transcript:

1 Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle

2 Data Input and Output Inputting data from the keyboard and outputting it to the screen using the software that comes with our book is similar to doing so using the features added to the Java programming language in Java 5. We’ve already seen some examples of inputting and outputting different kinds of data in the lab 1 and 2 programs.

3 Inputting Different Kinds of Numbers Integers Use the readint() method to input integers. Use the readint() method to input integers. Example from Operators.java : input in = new input(); output out = new output(); out.writeln("Enter first number " + "(an integer, please)"); int first = in.readint(); Example from Operators.java : input in = new input(); output out = new output(); out.writeln("Enter first number " + "(an integer, please)"); int first = in.readint(); Important Note: The type of the variable must match the type of the data the method is expecting.

4 Inputting Different Kinds of Numbers Floats Use the readfloat() method to input floats (floating- point numbers ranging from ±3.4 × 10 38 to ±1.4 × 10 -45 ). Use the readfloat() method to input floats (floating- point numbers ranging from ±3.4 × 10 38 to ±1.4 × 10 -45 ). Example from Time.java : input in = new input(); output out = new output(); out.writeln("Please enter distance (in miles)"); float distance = in.readfloat(); Example from Time.java : input in = new input(); output out = new output(); out.writeln("Please enter distance (in miles)"); float distance = in.readfloat(); Note: Note: Floating-point numbers got their name because they are stored internally in scientific notation. This requires “floating” the decimal point to a new location and adjusting the exponent (e.g., rewriting.0625 as 6.25 × 10 -2 ).

5 Inputting Different Kinds of Numbers Doubles Use the readdouble() method to input doubles (floating-point numbers ranging from ±1.8 × 10 308 to ±4.9 × 10 -324 ). Use the readdouble() method to input doubles (floating-point numbers ranging from ±1.8 × 10 308 to ±4.9 × 10 -324 ). Example from Tax.java : input in = new input(); output out = new output(); out.writeln("Program to compute tax"); out.writeln("Enter price of item:"); double price = in.readdouble(); Example from Tax.java : input in = new input(); output out = new output(); out.writeln("Program to compute tax"); out.writeln("Enter price of item:"); double price = in.readdouble();

6 Inputting Characters Characters Use the read() method to input a single character. Use the read() method to input a single character. For example, given the input ‘Y’ the statement: char choice = in.read(); would store ‘Y’ in choice. Use the readnext() method to input a single character and skip it. Use the readnext() method to input a single character and skip it.

7 Inputting Strings Strings Use the readname() method to input a string consisting of letters, digits, and underscores. Use the readname() method to input a string consisting of letters, digits, and underscores. The method reads until it encounters a character that is not a letter, digit, or underscore. For example, given the input “Ah! I see.” the statement: String name = in.readname(); would store “Ah” in name. Use the readword() method to input a string consisting of nonblank characters. Use the readword() method to input a string consisting of nonblank characters. The method reads until it encounters a blank character (Blank characters include the space and newline characters.). For example, given the input “Ah! I see.” the statement: String word = in.readword(); would store “Ah!” in word.

8 Inputting Strings Strings Use the readline() method to input a string consisting of nonblank as well as blank characters. Use the readline() method to input a string consisting of nonblank as well as blank characters. The method reads until it encounters the newline character (which it reads but does not store). For example, given the input “Ah! I see.” the statement: String line = in.readline(); would store “Ah! I see.” in line. Use the readln() method to input a string and skip it. Use the readln() method to input a string and skip it. The method reads until it encounters the newline character; it skips all characters, including the newline. For example, the statement: in.readln(); would read and skip any input string ending in newline.

9 Inputting Strings Strings Use the readblanks() method to input and skip consecutive blanks. Use the readblanks() method to input and skip consecutive blanks. For example, the statement: in.readblanks(); would read and skip consecutive blanks in an input string.

10 Outputting Different Kinds of Data Numbers, characters, strings Use the write() or writeln() methods to output numbers, characters, or strings. Use the write() or writeln() methods to output numbers, characters, or strings. write() outputs the expression specified in the parentheses. write() outputs the expression specified in the parentheses. writeln() outputs the expression specified in the parentheses followed by a newline character. writeln() outputs the expression specified in the parentheses followed by a newline character. Example 1: Example 1: input in = new input(); output out = new output(); out.write("Please enter an integer: "); int intNum = in.readint(); out.writeln("The integer you entered is " + intNum);

11 Outputting Different Kinds of Data Numbers, characters, strings Example 2: Example 2: input in = new input(); output out = new output(); char space = ‘ ’; out.writeln("Please enter your first name: "); String firstName = in.readname(); out.writeln("Please enter your last name: "); String lastName = in.readname(); out.writeln("You entered your name as: " + firstName + space + lastName); Concatenation: Glueing the same or different types (String, char) together using the ‘+’ operator to form a single output string


Download ppt "Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle."

Similar presentations


Ads by Google