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

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Lecture 15: I/O and Parsing
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Introduction to Objects and Input/Output
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
©Brooks/Cole, 2001 Chapter 11 Strings. ©Brooks/Cole, 2001 Figure 11-1.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Chapter 3b Standard Input and Output Sample Development.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
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.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
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 1.5 The New Java Mike Orsega Central Carolina CC.
Lecture 2 Objectives Learn about objects and reference variables.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Text Processing and More about Wrapper Classes Starting.
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.
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.
Lecture 5 I/O and Parsing
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Ihsan Ayyub Qazi Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved File I/O in java chapter.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Introduction to Computer Science and Object-Oriented Programming
INTERMEDIATE PROGRAMMING USING JAVA
Java Programming Lecture 2
Objectives You should be able to describe: Interactive Keyboard Input
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Interactive Standard Input/output
Exercise on Java Basics
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Message, Input, Confirm, and Specialized Dialogs
Chapter 3: Introduction to Objects and Input/Output
Input and Output in Java
Introduction to Classes and Methods
Input and Output in Java
Input and Output in Java
Object Oriented Programming
CS2011 Introduction to Programming I Strings
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

©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 Enter Data; System.out Is Used to Display Data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 System.in System.in is an InputStream –InputStream has a method called read that returns a byte –Bytes need to be combined to get useful pieces of infromation There are several classes we can use to get input in a more convenient form –InputStreamReader and BufferedReader together allow us to read one line at a time into a String Need to do a conversion to get primitive values –Scanner allows us to read Strings and primitive values directly

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scanner class New in Java 1.5 Located in the java.util package Create a Scanner from System.in to get input from the keyboard Scanner kbd; kbd = new Scanner(System.in); Now use the Scanner methods to read data from the keyboard

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scanner Methods MethodReturns nextInt()int value nextLong()long value nextFloat()float value nextDouble()double value next()one word (whitespace separated) nextLine()remainder of current line

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Example int n = kbd.nextInt(); double d = kbd.nextDouble() String word = kbd.next(); String text = kbd.nextLine();

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Tokenizing Strings The String class has some methods that can be used to break long Strings into pieces. We'll look at substring and indexOf later StringTokenizer can be used to break up strings that are lists By default, StringTokenizer uses spaces to delimit the tokens You can specify a different delimiter

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Tokenizing Strings Sometimes you will have a single String that contains several pieces of data –from a JOptionPane.showInputDialog which asks for more than one number at a time –if the input to your program has different formats depending on how the line starts We call the pieces of the String tokens.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Approaches The String class has some methods that can be used for tokenizing a String –substring, indexOf There is a class called StringTokenizer which makes the process easier –in the java.util package

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 StringTokenizer Create a StringTokenizer with a String argument StringTokenizer st = new StringTokenizer( textString) Methods –countTokens() returns the number of tokens –next() –hasNext() returns true if there is another token

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Parsing Tokens from a String Create a StringTokenizer object from the String Use the next() method of the StringTokenizer to get the pieces of the String Use the parse methods of the appropriate Wrapper class to convert the tokens to numeric values if necessary

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Conversions between Strings and Numbers Sometimes we have Strings that represent numeric values –The result of JOptionPane.showMessageDialog Sometimes, we want to insert a number into a String –for outputting results for example

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Wrapper Classes There is a Wrapper class associated with each primitive type –Used to create objects from primitive values –Class methods allow you to convert String to primitive type convert primitive type to String

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Wrapper Class Methods ClassString -> PrimitivePrimitive -> String Integerint Integer.parseInt( String)Integer.toString(int) Longlong Long.parseLong( String)Long.toString( long) Floatfloat Float.parseFloat( String)Float.toString( float) Doubledouble Double.parseDouble( String) Double.toString( double)