COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
String Tokenization What is String Tokenization?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Chapter 2 - Introduction to Java Applications
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
Chapter 3b Standard Input and Output Sample Development.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Chapter 3: Introduction to Objects and Input/Output.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage.
CIS 260: App Dev I. 2 Objects and Reference Variables n Predefined Java classes you have used:  String —for defining and manipulating strings  Integer.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
I/O in Java Dennis Burford
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Introduction to Programming
Lecture 2 Objectives Learn about objects and reference variables.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
Files and console applications Chapter 17 This chapter explains: What a file is How to read and write to files How to manipulate folder paths and file.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSC 142 H 1 CSC 142 Standard input/output. CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Java Programming Lecture 2
Outline Creating Objects The String Class Packages Formatting Output
Introduction to Classes and Objects
Objectives You should be able to describe: Interactive Keyboard Input
2.5 Another Java Application: Adding Integers
COMP 14 Introduction to Programming
Interactive Standard Input/output
Streams and File I/O Chapter 14.
Chapter 3: Introduction to Objects and Input/Output
Reading and Writing Text Files
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
Introduction to Java Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Announcements Assignment simpler than I thought (darn). Office hours (I’m lonely). Precedence of % operator. Why reference variables?

Review Assignment Operators count += 5;// count = count + 5; count -= 5; // count = count - 5; count *= 5; // count = count * 5; count /= 5; // count = count / 5;

Review Primitive Variables –primitive data types (int, double,...) –stores the data in the memory location Reference Variables –stores an address in the memory location –"points to" another memory location

Today in COMP 14 Input/Output using GUI String Tokenization Format Floating-Point Output Read from and Write to Files

Using Dialog Boxes for I/O Use a graphical user interface (GUI) class JOptionPane –Contained in package javax.swing –showInputDialog allows user to input a string from the keyboard –showMessageDialog allows the programmer to display results Program must end with System.exit(0);

JOptionPane Methods showInputDialog str = JOptionPane.showInputDialog(strExpression); –stores what the user enters into the String str showMessageDialog JOptionPane.showMessageDialog(parentComponent, strExpression, boxTitleString, messageType);

showMessageDialog parentComponent –parent of the dialog box –we'll use null StrExpression –what you want displayed in the box boxTitleString –title of the dialog box messageType –what icon will be displayed

messageType JOptionPane.ERROR_MESSAGE –error icon JOptionPane.INFORMATION_MESSAGE –information icon JOptionPane.PLAIN_MESSAGE –no icon JOptionPane.QUESTION_MESSAGE –question mark icon JOptionPane.WARNING_MESSAGE –exclamation point icon

JOptionPane Example JOptionPane.showMessageDialog (null, "Hello World!", "Greetings", JOptionPane.INFORMATION_MESSAGE);

UsingGUI.java example

User Input BufferedReader –reads everything as a string Integer.parseInt –only handles one integer in the string How to handle? Enter 3 numbers:

The StringTokenizer Class tokens –elements that comprise a string tokenizing –process of extracting these elements delimiters –characters that separate one token from another StringTokenizer class –defined in the java.util package –used to separate a string into tokens

The StringTokenizer Class Tokens and Delimiters "Four score and seven years ago" "Bart:Lisa:Homer:Marge" delimiter: ' ' tokens: "Four" "score" "and" "seven" "years" "ago" delimiter: ':' tokens: "Bart" "Lisa" "Homer" "Marge"

The StringTokenizer Class Default delimiters: –space, tab, carriage return, new line Methods –StringTokenizer (String str) –StringTokenizer (String str, String delimits) –String nextToken() –boolean hasMoreTokens() –int countTokens()

Tokenize.java example separated by spaces separated by commas –gotcha

Formatting the Output of Decimal Numbers float: defaults to 6 decimal places double: defaults to 15 decimal places

class DecimalFormat Import package java.text Create DecimalFormat object and initialize DecimalFormat fmt = new DecimalFormat (formatString); FormatString –"0.00" - limit to 2 decimal places, use 0 if there's no item in that position –"0.##" - limit to 2 decimal places, no trailing 0 Use method format –rounds the number instead of truncating Result of using DecimalFormat is a String

Examples DecimalFormat twoDecimal = new DecimalFormat("0.00"); DecimalFormat fmt = new DecimalFormat("0.##"); System.out.println (twoDecimal.format(56.379)); System.out.println (fmt.format(56.379)); System.out.println (twoDecimal.format(.3451)); System.out.println (fmt.format(.3451)); System.out.println (twoDecimal.format(.3)); System.out.println (fmt.format(.3));

Reading From Text Files Similar to reading from the keyboard Create a BufferedReader object, but use a FileReader object instead of InputStreamReader Create BufferedReader object inside the main method instead of outside Substitute the name of the file for System.in When finished reading from the file, we need to close the file: –BufferedReader close() method

Exceptions FileNotFoundException –if the file specified to open was not found IOException –some other I/O exception public static void main (String[] args) throws FileNotFoundException, IOException

Reading From Text Files String file = "data.dat"; BufferedReader inFile = new BufferedReader (new FileReader (file)); String line = inFile.readLine(); inFile.close();

Writing To Text Files Similar to reading from text files Use FileWriter and PrintWriter instead of FileReader and BufferedReader PrintWriter –methods include print() and println(), which we use just like those in System.out Like reading, we need to close the file when we're done –PrintWriter close() method

Writing To Text Files String file = "outfile.dat"; PrintWriter outFile = new PrintWriter (new FileWriter (file)); outFile.print ("Hi"); outFile.println(" There!"); outFile.close();

FileGUI.java Example

Summary JOptionPane –showInputDialog –showMessageDialog StringTokenizer –tokens are separated by a delimiter DecimalFormat –pattern tells how many digits after the decimal point –0 - fill in with trailing 0s –# - don't fill in trailing 0s

Summary Reading Data from File –BufferedReader –FileReader readLine() Writing Data to a File –FileWriter –PrintWriter print(StrExpression) println(StrExpression) Close files after using them

Next Time in COMP 14 Reading Assignment Chapter 4 (pgs ) Relational and Logical Operators and Expressions