Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,

Similar presentations


Presentation on theme: "Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,"— Presentation transcript:

1 Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved

2 Objectives Understand the Text I/O Lab and how we can use the TextIO library (API) in our text Enhance our understanding of how to use already written classes to create applications Let’s get started!

3 Program GraphicalInput.java How do we modify it to handle int types? Integer class and (int) typecasting

4 Program PrintSquare.java How do we modify it to handle double types? Check the TextIO API for getDouble()

5 Understanding the TextIO API System.out.print versus System.out.println Method overloading: the above methods print a double, an int, a string, etc. TextIO.put versus TextIO.putln http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4.1/c2/s4.html We can format output better with these methods: As mentioned above, the TextIO subroutines TextIO.put and TextIO.putln can be used as replacements for System.out.print and System.out.println. However, TextIO goes beyond System.out by providing additional, two-parameter versions of put and putln. You can use subroutine call statements of the form "TextIO.put(x,n);" and "TextIO.putln(x,n);", where the second parameter, n, is an integer-valued expression. The idea is that n is the number of characters that you want to output. If x takes up fewer than n characters, then the computer will add some spaces at the beginning to bring the total up to n. (If x already takes up more than n characters, the computer will just print out more characters than you ask for.) This feature is useful, for example, when you are trying to output neat columns of numbers, and you know just how many characters you need in each column.

6 Understanding the TextIO API Java does not have a great API for receiving input from keyboard. The TextIO API helps us a lot here. b = TextIO.getByte(); // value read is a byte i = TextIO.getShort(); // value read is a short j = TextIO.getInt(); // value read is an int k = TextIO.getLong(); // value read is a long x = TextIO.getFloat(); // value read is a float y = TextIO.getDouble(); // value read is a double a = TextIO.getBoolean(); // value read is a boolean c = TextIO.getChar(); // value read is a char w = TextIO.getWord(); // value read is a String s = TextIO.getln(); // value read is a String For these statements to be legal, the variables on the left side of each assignment statement must be of the same type as that returned by the function on the right side. When you call one of these functions, you are guaranteed that it will return a legal value of the correct type. If the user types in an illegal value as input -- for example, if you ask for a byte and the user types in a number that is outside the legal range of -128 to 127 -- then the computer will ask the user to re-enter the value, and your program never sees the first, illegal value that the user entered.

7 Understanding the TextIO API You'll notice that there are two input functions that return Strings. The first, getWord(), returns a string consisting of non-blank characters only. When it is called, it skips over any spaces and carriage returns typed in by the user. Then it reads non-blank characters until it gets to the next space or carriage return. It returns a String consisting of all the non-blank characters that it has read. The second input function, getln(), simply returns a string consisting of all the characters typed in by the user, including spaces, up to the next carriage return. It gets an entire line of input text. The carriage return itself is not returned as part of the input string, but it is read and discarded by the computer. Note that the String returned by this function might be the empty string, "", which contains no characters at all. All the other input functions listed -- getByte(), getShort(), getInt(), getLong(), getFloat(), getDouble(), getBoolean(), and getChar() -- behave like getWord(). That is, they will skip past any blanks and carriage returns in the input before reading a value. However, they will not skip past other characters. If you try to read two ints and the user types "2,3", the computer will read the first number correctly, but when it tries to read the second number, it will see the comma. It will regard this as an error and will force the user to retype the number. If you want to input several numbers from one line, you should make sure that the user knows to separate them with spaces, not commas. Alternatively, if you want to require a comma between the numbers, use getChar() to read the comma before reading the second number.

8 Understanding the TextIO API The semantics of input is much more complicated than the semantics of output. The first time the program tries to read input from the user, the computer will wait while the user types in an entire line of input. TextIO stores that line in a chunk of internal memory called the input buffer. Input is actually read from the buffer, not directly from the user's typing. The user only gets to type when the buffer is empty. This lets you read several numbers from one line of input. However, if you only want to read in one number and the user types in extra stuff on the line, then you could be in trouble. The extra stuff will still be there the next time you try to read something from input. (The symptom of this trouble is that the computer doesn't pause where you think it should to let the user type something in. The computer had stuff left over in the input buffer from the previous line that the user typed.) To help us avoid this, there are versions of the TextIO input functions that read a data value and then discard any leftover stuff on the same line: b = TextIO.getlnByte(); // value read is a byte i = TextIO.getlnShort(); // value read is a short j = TextIO.getlnInt(); // value read is an int k = TextIO.getlnLong(); // value read is a long x = TextIO.getlnFloat(); // value read is a float y = TextIO.getlnDouble(); // value read is a double a = TextIO.getlnBoolean(); // value read is a boolean c = TextIO.getlnChar(); // value read is a char w = TextIO.getlnWord(); // value read is a String Note that calling getlnDouble(), for example, is equivalent to first calling getDouble() and then calling getln() to read any remaining data on the same line, including the end-of-line character itself. We are strongly advised to use the "getln" versions of the input routines, rather than the "get" versions, unless we really want to read several items from the same line of input.

9 Fun In Class Assignment Write a java program called UnderstandTextIO.java that uses the TextIO API to: -read from the keyboard into variables of the following types: char, int, double, String with a space between each item value - read from the keyboard into variables of the following types: char, int, double, String with a carriage return between each item value -read a line of input from the keyboard as a String and locate the position of the first blank in the String (hint: use indexof(ch) method that is part of String class) - print out the Unicode values of ‘?’, ‘\n’, ‘a’ and ‘A’

10 We understood how to use the TextIO API by examining the Text I/O lab We enhanced our understanding of how to use existing code to write applications by examining the TextIO API CONCLUSION


Download ppt "Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,"

Similar presentations


Ads by Google