Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01.

Similar presentations


Presentation on theme: "Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01."— Presentation transcript:

1 Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01

2 OutlineOutline ä Overview ä Input classes ä InputStreamReader ä BufferedReader ä Reading keyboard input ä Reading Strings ä Converting to other types ä Reading from text files ä Overview ä Input classes ä InputStreamReader ä BufferedReader ä Reading keyboard input ä Reading Strings ä Converting to other types ä Reading from text files

3 OverviewOverview ä A stream is a technical term for a whole sequence of text input. (Think of it as a stream of characters.) ä Whenever Java reads input (from the keyboard, from a file, etc.), it takes in a stream and stores it in what’s called a Buffer: ä A Buffer is an object that holds each of the characters read in from a stream. To be of any use to us, the Buffer must be converted to a String. ä A stream is a technical term for a whole sequence of text input. (Think of it as a stream of characters.) ä Whenever Java reads input (from the keyboard, from a file, etc.), it takes in a stream and stores it in what’s called a Buffer: ä A Buffer is an object that holds each of the characters read in from a stream. To be of any use to us, the Buffer must be converted to a String. Hello

4 Input Classes ä Java has two classes that we’ll need to use to read input from the keyboard:  InputStreamReader is a class that’s used to read streams from the keyboard and save them as Buffers.  BufferedReader is a class that’s used to convert a Buffer to a String. ä These classes aren’t automatically available for our use.  To use them, we need the following command at the very beginning of our program: import java.io.*;  import is a keyword that tells Java to load certain classes for our use. ä Java has two classes that we’ll need to use to read input from the keyboard:  InputStreamReader is a class that’s used to read streams from the keyboard and save them as Buffers.  BufferedReader is a class that’s used to convert a Buffer to a String. ä These classes aren’t automatically available for our use.  To use them, we need the following command at the very beginning of our program: import java.io.*;  import is a keyword that tells Java to load certain classes for our use.

5 Reading Keyboard Input  Before we can read input from the keyboard, we must create a new object of the InputStreamReader class, like this: InputStreamReader stream = new InputStreamReader(System.in);  Then we must create a new object of the BufferedReader class, like this: BufferedReader buffer = new BufferedReader(stream);  Finally, we can read input using the instance method readLine() of the class BufferedReader : ä String input; input = buffer.readLine();  Before we can read input from the keyboard, we must create a new object of the InputStreamReader class, like this: InputStreamReader stream = new InputStreamReader(System.in);  Then we must create a new object of the BufferedReader class, like this: BufferedReader buffer = new BufferedReader(stream);  Finally, we can read input using the instance method readLine() of the class BufferedReader : ä String input; input = buffer.readLine();

6 Converting to Other Types ä Input is always read into Strings. Even if we enter the number 3, Java stores the String “3”.  We can convert to an int like this: ä int inputInt; inputInt = Integer.parseInt(input);  And we can convert to a double like this: ä double inputDouble; ä inputDouble = Double.valueOf(input).doubleValue(); ä Input is always read into Strings. Even if we enter the number 3, Java stores the String “3”.  We can convert to an int like this: ä int inputInt; inputInt = Integer.parseInt(input);  And we can convert to a double like this: ä double inputDouble; ä inputDouble = Double.valueOf(input).doubleValue();

7 Reading from Text Files ä What if we have text (or any data) that has been stored in a file? ä We can read that as input much like we read keyboard input. ä There’s another class that we’ll need to read input from a text file:  FileReader is a class that’s very similar to InputStreamReader – it’s used to read streams from a text file. ä What if we have text (or any data) that has been stored in a file? ä We can read that as input much like we read keyboard input. ä There’s another class that we’ll need to read input from a text file:  FileReader is a class that’s very similar to InputStreamReader – it’s used to read streams from a text file.

8 Reading from Text Files  Instead of creating an InputStreamReader object, we create a FileReader object, like this: FileReader file = new FileReader(“data.txt”); ä In this case, data.txt is the name of our text file – this can be any name we want it to be.  Now, we can use the FileReader object file just like we used the InputStreamReader object stream before: ä BufferedReader buffer = new BufferedReader(file); String input = buffer.readLine();  Instead of creating an InputStreamReader object, we create a FileReader object, like this: FileReader file = new FileReader(“data.txt”); ä In this case, data.txt is the name of our text file – this can be any name we want it to be.  Now, we can use the FileReader object file just like we used the InputStreamReader object stream before: ä BufferedReader buffer = new BufferedReader(file); String input = buffer.readLine();

9 HomeworkHomework ä Read: 13.4, to the bottom of page 568. ä HW6 out today, due Friday. ä Calculator. ä Read: 13.4, to the bottom of page 568. ä HW6 out today, due Friday. ä Calculator.


Download ppt "Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01."

Similar presentations


Ads by Google