Characters, Strings, Reading Files CS2110, Week 2 Recitation.

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

Lecture 15: I/O and Parsing
Packages, Characters, Strings Arguments to method main CS2110, Week 2 Recitation 1.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
String Escape Sequences
Reading/Writing Files, Webpages CS2110, Recitation 10 1.
1 Course Lectures Available on line:
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
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.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
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.
From C++ to Java A whirlwind tour of Java for C++ programmers.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Lecture 5 I/O and Parsing
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.
Reading/Writing Files, Webpages CS2110, Recitation 8 1.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
17-Feb-16 String and StringBuilder Part I: String.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Arguments to method main, Packages, Wrapper Classes, Characters, Strings CS2110, Recitation 2.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Arguments to method main Packages APIspecs Characters Strings
CS2110, Recitation 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings.
String class.
Introduction to Computer Science / Procedural – 67130
CS2110, Recitation 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings.
Program Input/Output (I/O)
Multiple variables can be created in one declaration
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
String and StringBuilder
Escape Sequences What if we wanted to print the quote character?
Object Oriented Programming
String and StringBuilder
Chapter 2: Basic Elements of Java
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
String and StringBuilder
String and StringBuilder
String methods 26-Apr-19.
Switch, Strings, and ArrayLists in Java
Presentation transcript:

Characters, Strings, Reading Files CS2110, Week 2 Recitation

Primitive type char char fred = 'a'; char wilma = 'b'; System.out.println(fred); Unicode: 2-byte representation Visit to see all unicode charswww.unicode.org/charts/ a Use single quotes

Special chars worth knowing about ' ' - space '\t' - tab character '\n' - newline character '\'' - single quote character '\"' - double quote character '\\' - backslash character '\b' - backspace character - NEVER USE THIS '\f' - formfeed character - NEVER USE THIS '\r' - carriage return - NEVER USE THIS Backslash, called the escape character

Casting char values (int) 'a' gives 97 (char) 97 gives 'a' (char) 2384 gives ' ॐ ' Cast a char to an int using unary prefix operator (int), Gives unicode representation of char, as an int No operations on chars (values of type char)! BUT, if used in relation or arith, a char is automatically cast to type int. Relations = == != == 'a' < 'b' same as 97 < 98, i.e. false 'a' + 1 gives 98 Om, or Aum, the sound of the universe (Hinduism)

Class Character An object of class Character wraps a single char (has a field that contains the char) Character c1= new Character('b'); Character c2= new Character('c'); ??? 'b' charValue() compareTo(Character) equals(Object) ??? 'c' charValue() compareTo(Character) equals(Object) c1 c2 Don’t know field name

Class Character Each instance of class Character wraps an int value —has a field that contains an int value. Allows a char value to be treated as an object Find methods in each object by looking at API specs on web: docs.oracle.com/javase/7/docs/api/java/lang/Character.html c.charValue() c.equals(c1) c.compareTo(c1) c.toString() … c’s wrapped char, as a char True iff c1 is a Character and wraps same char 0 if c==c1. 0 if c > c1 c’s wrapped char, as a String …

Static methods in class Character Lots of static functions. You have to look to see what is available. Below are examples isAlphabetic(c) isDigit(c) isLetter(c) isLowerCase(c) isUpperCase(c) isWhitespace(c) toLowerCase(c) toUpperCase(c) These return the obvious boolean value for parameter c, a char Whitespace chars are the space ‘ ‘, tab char, line feed, carriage return, etc. These return a char.

== versus equals c1 == c2 c3 == c1 c1 == c1 c1.equals(c2) c3.equals(c1) ??? 'b' charValue() compareTo(Character) equals(Object) ??? 'b' charValue() compareTo(Character) equals(Object) c1 c2 null c3 true iff c1, c2 contain same values true iff c2 is also a Character object and contains same char as c1 false true Error!!!

“CS211” Class String String s = “CS211”; ??? String: special place in Java: no need for a new-expression. This creates object. s length() charAt(int) subString(int) subString(int, int) equals(Object) trim() contains(String) indexOf(String) startsWith(String) endsWith(String) … more … Find out about method of class String: docs.oracle.com/javase/7/docs/api/ index.html?java/lang/String.html Lots of methods. We explain basic ones Important: String object is immutable: can’t change its value. All operations/functions create new String objects

Operator + "abc" + "12$" evaluates to "abc1 2$" Catenation, or concatenation + is overloaded (1 + 2) + "ab$" evaluates to "3ab$ " If one operand of catenation is a String and the other isn’t, the other is converted to a String. Sequence of + done left to right "ab$ " evaluates to " ab$12 "

Operator + System.out.println("c is: " + c + “, d is: " + d + “, e is: " + e); c 32 d -3 e 201 Output: c is: 32, d is: -3, e is: 201 Can use + to advantage in println statement. Good debugging tool. Note how each output number is annotated to know what it is. Using several lines increases readability

s.substring(i): new String containing chars at positions from i to end — s.substring(2) is ' 13 ' Picking out pieces of a String s.length(): number of chars in s — 5 " CS 13 " ? s length() charAt(int) subString(int) subString(int, int) … more … " CS 13 " Numbering chars: first one in position 0 s.char(i): char at position i of s — s.charAt(3) is ' 1 ' s.substring(i,j): new String containing chars at positions i..(j-1) — s.substring(2,4) is ' 13 ' Be careful: Char at j not included!

Other useful String functions s.trim() – s but with leading/trailing whitespace removed s.indexOf(s1) – position of first occurrence of s1 in s (-1 if none) s.lastIndexOf(s1) – similar to s.indexOf(s1) s.contains(s1) – true iff String s1 is contained in s2 s.startsWith(s1) – true iff s starts with String s1 s.endsWith(s1) – true iff s ends with String s1 s.compareTo(s1) – 0 if s and s1 contain the same string, 0 if s is greater (dictionary order) There are more functions! Look at the API specs!

Reading files On the first assignment, your program will read a file from your hard drive. We will give you the code to hook up to a file and show you what method to read the next line, but some intro to I/O will help. I/O classes are in package java.io. To import the classes so you can use them, put import java.io.*; Before the class declaration

Input Streams Stream: a sequence of data values that is processed —either read or written— from beginning to end. We are dealing with input streams. Read input stream for a file is by creating an instance of class FileReader: FileReader fr= new FileReader(arg that describes file to read); fr.read() // get next char of file Too low-level! Don’t want to do char by char.

Reading a line at a time Class BufferedReader, given a FileReader object, provides a method for reading one line at a time. FileReader fr= new FileReader(arg that describes file to read); BufferedReader br= new BufferedReader(fr); Then: String s= br.readLine(); // Store next line of file in s0 When finished with reading a file, it is best to close it! br.close();

Argument describing file to read FileReader fr= new FileReader(arg that describes file to read); 1.Can be a String –the name of the file. Relative to current directory 2.Can be a String that gives the path to the file as well as the file, e.g. on a Macintosh: "/Users/gries/Work/Eclipse/a0/SpeciesData/A12.dat” 3. Can be an object of class File.

Argument describing file to read /** Return number of lines in f. Throw IO Exception if problems encountered when reading */ public static void printSize(File f) throws IOException { FileReader fr= new FileReader(f); BufferedReader br= new BufferedReader(fr); int n= 0; // number of lines read so far String line= br.readLine(); while (line != null) { n= n+1; line= br.readLine(); } br.close(); return n; }