21-Jun-15 Simple Text I/O. java.util.Scanner Java finally has a fairly simple way to read input First, you must create a Scanner object To read from the.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lecture 3 Java Basics Lecture3.ppt.
Some basic I/O.
26-Jun-15 Simple Text I/O. java.util.Scanner Java finally has a fairly simple way to read input First, you must create a Scanner object To read from the.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
1 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,
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.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
The Scanner Class and Formatting Output Mr. Lupoli.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
COMP String and Console I/O Yi Hong May 18, 2015.
File I/O (Input and Output). Why use files? Things stored in files are more permanent than things stored in variables in programs. Things stored in files.
3-Jun-16 Simple Java I/O Part I General Principles.
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.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
 byte  short  int  long  float  double  boolean  char.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
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.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Part I General Principles
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to programming in java
Input/Output.
Objectives You should be able to describe: Interactive Keyboard Input
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
Lecture Notes – Basics (Ch1-6)
Text File Input/Output
User Input You’ve seen this: A Scanner object:
Chapter 3 Java Input/Output.
Simple Java I/O part I Using scanner 8-Nov-18.
Program Style Console Input and Output
API, Wrapper classes, printf, and methods
Unit-2 Objects and Classes
Input and Output in Java
Introduction to Classes and Methods
Chapter 3 Input/Output.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
Input and Output in Java
Java Programming Review 1
A+ Computer Science INPUT.
Input and Output in Java
The keyboard is the standard input device.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.
Presentation transcript:

21-Jun-15 Simple Text I/O

java.util.Scanner Java finally has a fairly simple way to read input First, you must create a Scanner object To read from the keyboard ( System.in ), do: Scanner scanner = new Scanner(System.in); To read from a file, do: File myFile = new File("myFileName.txt"); Scanner scanner = new Scanner(myFile); You have to be prepared to handle a FileNotFound exception You can even “read” from a String: Scanner scanner = new Scanner(myString); This can be handy for parsing a string

Using the Scanner First, you should make sure there is something to scan scanner.hasNext()  boolean You wouldn’t use this when reading from the keyboard You can read a line at a time scanner.nextLine()  String Or, you can read one “token” at a time A token is any sequence of nonwhitespace characters scanner.next ()  String You must be prepared to deal with exceptions Eclipse will tell you what you need to do These return Strings, which you can convert to numbers or other types if you like There are also methods to check for and return primitives directly

Scanning for primitives You can read in and convert text to primitives: boolean b = sc.nextBoolean(); byte by = sc.nextByte(); short sh = sc.nextShort(); int i = sc.nextInt(); long l = sc.nextLong(); float f = sc.nextFloat(); double d = sc.nextDouble(); And test if you have something to read: hasNextBoolean() hasNextByte() hasNextShort() hasNextInt() hasNextLong() hasNextFloat() hasNextDouble()

Formatted output Java 5 has a printf method, similar to that of C Each format code is % width code Some format codes are s for strings, d for integers, f for floating point numbers Example: double pi = Math.PI; System.out.printf("%8s %-8s %6d %-6d %8f %-8.2f :) \n", "abc", "def", 123, 456, pi, pi); System.out.printf("%8s %-8s %6d %-6d", "abcdef", "ghijkl", 12345, 6789); Output: abc def :) abcdef ghijkl

But wait…there’s more We have just scratched the surface of the Scanner and Formatter classes See the Java API for more details

The End