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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
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.
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.
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,
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!");
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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)
CIS 270—Application Development II Chapter 28—Formatted Output.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
3-Jun-16 Simple Java I/O Part I General Principles.
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.
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.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A: A: double “4” A: “34” 4.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
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.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
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.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
28 Formatted Output.
Part I General Principles
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)
User Input You’ve seen this: A Scanner object:
Chapter 3 Java Input/Output.
Programming in C Input / Output.
Program Style Console Input and Output
Review Operation Bingo
API, Wrapper classes, printf, and methods
Unit-2 Objects and Classes
Introduction to Classes and Methods
Introduction to Primitive Data types
Chapter 3 Input/Output.
Differences between Java and C
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
The keyboard is the standard input device.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Introduction to Primitive Data types
Presentation transcript:

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 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 System.out.println(Math.PI); will print out If you want to print out this number as , or 3.14, you need to format it If you want to print out numbers in neat columns, you need to format them Prior to Java 1.5, you had to figure out how to do this yourself Java 1.5 introduced the Formatter class to do formatting for you In typical Java style, Formatter can do just about anything—but doesn’t try to make the common things easy For the most part, we won’t use the Formatter class directly, but will use System.out.format(...)

Formatted output Java 5 has a printf method, similar to that of C Each format code is % width code The width is the number of characters that are output (with blank fill) By default, output is right-justified A negative width means to left-justify the output Some values for the code are s for strings, d for integers, f for floating point numbers, b for booleans For floating point numbers, the width has the form total.right, where total is the total width and right is the number of digits to the right of the decimal point There are a huge number of options for formatting dates, which we won’t cover

Examples System.out.printf("Left justified: |%-8s|\n", "abc"); System.out.printf("Right justified: |%8s|\n", "abc"); System.out.printf("Left justified: |%-8d|\n", 25); System.out.printf("Right justified: |%8d|\n", 25); System.out.printf("Left justified: |%-8.4f|\n", Math.PI); System.out.printf("Right justified: |%8.4f|\n", Math.PI); System.out.format("Left justified: |%-8.2f|\n", Math.PI); System.out.format("Right justified: |%8.2f|\n", Math.PI); System.out.format("Left justified: |%-8b|\n", true); System.out.format("Right justified: |%8b|\n", true); Left justified: |abc | Right justified: | abc| Left justified: |25 | Right justified: | 25| Left justified: | | Right justified: | | Left justified: |3.14 | Right justified: | 3.14| Left justified: |true | Right justified: | true|

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