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

Slides:



Advertisements
Similar presentations
User Input. Why user entry? When we assign values (age = 16), we are making a program static…it never changes. If we ask the user to enter the values,
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Introduction to Python
General Programming Introduction to Computing Science and Programming I.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Looping and Counting Lecture 3 Hartmut Kaiser
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
User-Written Functions
Chapter 2 Basic Computation
Input/Output.
Strings (Continued) Chapter 13
Introduction to Computer Science / Procedural – 67130
Variables and Primative Types
Variables, Expressions, and IO
getline() function with companion ignore()
Chapter 2 Basic Computation
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
IDENTIFIERS CSC 111.
Conditions and Ifs BIS1523 – Lecture 8.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
CISC101 Reminders Assn 3 due tomorrow, 7pm.
IFS410 Advanced Analysis and Design
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Java Programming Review 1
Overloading functions
Fundamental Programming
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
CISC101 Reminders Assignment 3 due today.
Loops and Iteration CS 21a: Introduction to Computing I
getline() function with companion ignore()
Presentation transcript:

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

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!

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

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

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 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.

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 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.

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.

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.

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’

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