COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.

Slides:



Advertisements
Similar presentations
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©2004 Brooks/Cole Chapter 3 Interactive Input. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 2: Java Fundamentals Input and Output statements.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter 3b Standard Input and Output Sample Development.
1 Course Lectures Available on line:
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
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.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
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.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Objectives You should be able to describe: Interactive Keyboard Input
Interactive Standard Input/output
Chapter 3 Java Input/Output.
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
Introduction to Classes and Methods
A+ Computer Science INPUT.
Chapter 3 Numerical Data
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal Numbers *Wrapper Classes *Standard Input *GregorianCalendar

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Standard Output *The showMessageDialog method is intended for displaying short one-line messages, not for a general-purpose output mechanism. *Using System.out, we can output multiple lines of text to the standard output window - the console.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. System.out *System.out is a PrintStream object. *Two useful methods the print method takes a String as its argument and prints it to the console the println method takes a String as its argument and prints it to the console with a newline character appended to the end *Both methods will continue printing from the end of the currently displayed output. *Both methods will do the necessary type conversion if we pass numerical data.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Overloaded + Operator *The + operator can be used in 2 ways adding numbers concatenating Strings *What happens if we mix its arguments int x = 1; int y = 2; String output = “test” + x + y; String output = x + y + “test”;

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Output Example Printing a prompt on same line as input will be typed: System.out.print( "Enter radius: "); Echo printing the input using print and println: System.out.print( "The radius is "); System.out.println( radius); Printing results (using +): System.out.println( "Area : " + area);

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. DecimalFormat *Since floating point numbers can't be represented exactly in computer memory, they often print out with lots of digits after the decimal point. This can make the output look messy *The DecimalFormat class lets you print numbers with a fixed number of digits after the decimal place. *Use a "picture" string to show what you want the number to look like "0.000" means 3 digits after the decimal point *Use the format method with the number you want formatted as an argument

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. DecimalFormat Example *Create the DecimalFormat with a String DecimalFormat df = new DecimalFormat("0.000"); *Call the format method with the number as an argument double fp; /* assign a value to fp */ System.out.print( df.format(fp));

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Getting Numerical Input Values *Java provides primitive data types for working with numbers. *Wrapper classes exist that allow you to make an object out of a number. There is a wrapper class for each primitive type: Integer, Double, … *These classes provide methods we can use to convert an appropriate String object to a numerical value. Integer.parseInt() converts a String like "23" to an int Double.parseDouble() converts a String like "2.3" to a double

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Wrapper classes *Allow you to create an object from a primitive value *Are used to perform necessary type conversions Converting a String object to a numerical value Converting a numeric value to a String. *There is a wrapper class for each of the java primitive types

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Getting Numeric Input Data Data from the keyboard often comes into the program as a String String radiusStr = JOptionPane.showInputDialog(null, "Enter radius:"); A string containing numeric data needs to be converted before it can be used in calculations radius = Double.parseDouble(radiusStr);

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Standard Input *Using input dialogs is tedious for large amounts of input. And, not very user-friendly *The technique of using System.in to input data is called standard input. *System.in is an InputStream which handles byte data. *Using the intermediate InputStreamReader object allows us to read a single character at a time. *The BufferedReader class allows us to read one line at a time (in String form) We could read numbers with BufferedReader and wrapper classes

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The Scanner Class *Java1.5 has added a new input class that makes numeric input simpler. java.util.Scanner *Use System.in to create a Scanner Scanner kbd = new Scanner( System.in); Note: All the Scanner examples in the text need to be corrected.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Scanner input methods nextInt gets an int from the input int i = kbd.nextInt(); nextDouble double d = kbd.nextDouble(); nextByte, nextShort, nextLong and nextFloat work similarly next() gets a String reads characters until the next whitespace

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Reading line-by-line with Scanner *By default, Scanner uses white space to delimit (separate) Strings *To read an entire line need to replace the delimiter Ask System class what it is for your OS String delim = System.getProperties( "line.separator"); Tell the Scanner to use it kbd.useDelimiter( delim); Then read the line using next String line = kbd.next();

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The GregorianCalendar Class *The GregorianCalendar class is useful in manipulating calendar information such as year, month, and day. *You can create a GregorianCalendar for any date by giving the year, month and day cal = new GregorianCalendar(2001, 8, 11); Caution: the first month of the year, January, is represented by 0.

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. GregorianCalendar *getTime method returns a Date *get(Calendar.property) allows you to determine year, month, date, day of week, hours, minutes, … *Use SimpleDateFormat or DateFormat to output the date in the desired format *DateFormat has a parse method that takes a String in the proper format and returns a Date

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Extra Material *BufferedReader is covered later in your text

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Input classes *How the sequence of I/O objects adds greater capabilities. InputStream has the capability to read bytes InputStreamReader has the capablity to read characters (2 bytes) BufferedReader can read an entire line

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using BufferedReader *Declaration BufferedReader kbd *Creation kbd = new BufferedReader( new InputStreamReader( System.in)); *Use String input = kbd.readLine();

COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Handling Input Errors *Calling the readLine method of a BufferedReader can result in an error condition called an exception. *The programmer is required to handle this potential error condition. *For now we choose to ignore these problems by adding the clause throws IOException to the method declaration whenever a method includes a call to the readLine method. public static void main(String [] args) throws IOException {... String input = bufReader.readLine();... }