String and Scanner CS 21a: Introduction to Computing I First Semester, 2013-2014.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
Chapter 2: Java Fundamentals Input and Output statements.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
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.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Java Concepts Chapter 2 - Using Objects Mr. Smith AP Computer Science A.
Introduction to Programming
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
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 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Introduction to programming in java
Introduction to Computer Science and Object-Oriented Programming
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Input/Output.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
AKA the birth, life, and death of variables.
Exceptions and User Input Validation
Building Java Programs Chapter 4.3
Maha AlSaif Maryam AlQattan
Java Concepts Chapter 2 - Using Objects
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
Introduction to Computer Science and Object-Oriented Programming
Lecture 10 Strings CSE /26/2018.
Chapter 2: Java Fundamentals
The keyboard is the standard input device.
CS2011 Introduction to Programming I Strings
CS 1054 Introduction to Programming in Java
More on iterations using
Presentation transcript:

String and Scanner CS 21a: Introduction to Computing I First Semester,

The String Class ► String: a built-in class in Java ► Methods on String objects: ► public int length() ► public String toUpperCase() ► public String substring( int first, int last ) ► javap java.lang.String for a complete list ► Note: strings are immutable (no mutator methods) ► String objects have a special treatment in Java ► To enable string literals, string display, and string concatenation

Using Strings ► String literal example: "Hello, World" ► String variable: String message = "Hey"; // same as String message = new String("Hey"); ► Using strings: println( "Hello, World" ); println( message ); println( message.length() ); String caps = message.toUpperCase(); println( caps ); Prints: Hello, World Hey 3 HEY

Strings are Objects ► String variables contain object references String s = "Hey"; ► Calling length on a string int x = s.length(); s "Hey" length() "Hey" 3

Strings are Objects ► Calling toUpperCase on a string returns another string String caps = s.toUpperCase(); ► Note that the state of s does not change in this case toUpperCase() "Hey""HEY"

String Concatenation ► The + operator causes a concatenation if the operands are strings println( "basket" + "ball" ); ► If only one operand is a string, the other operand is first converted to a string and then a concatenation is performed int ans = 5; println( "The answer is " + ans ); Prints: basketball Prints: The answer is 5

Converting between Strings and Number Types ► Suppose int i = 5; double d = 10.0; String s = "7"; ► From String to number i = Integer.parseInt( s ); // assigns 7 to i d = Double.parseDouble( s ); // assigns 7.0 to d ► From number to String s = Integer.toString( i ); // assigns "5" to s s = Double.toString( d ); // assigns "10.0" to s ► Can also use concatenation s = "" + d;

The substring Method ► Each character in a String object has a position (starting with 0) ► The parameters for substring indicate: ► first: the starting letter of the substring of interest ► last: the position following the ending letter of the substring ► This way, (last-first) = the length of the resulting substring ► Example: String s = "Ateneo de Manila"; String a = s.substring( 0, 6 ); // "Ateneo" String b = s.substring( 6, 9 ); // " de" String c = s.substring( 10,16 ); // "Manila"

Reading Console Input and the Scanner Class ► To enable console input: ► Before the declaration of the application class, type: import java.util.Scanner; ► At the beginning of the main method of the Java application, create a scanner object: Scanner in = new Scanner( System.in ); ► Then, invoke methods on the Scanner object int i = in.nextInt(); double d = in.nextDouble(); String s = in.nextLine(); // reads an entire line of input String w = in.next(); // reads one word only

Input Example import java.util.Scanner; public class DollarToPesoConversion { public static void main( String args[] ) { Scanner in = new Scanner( System.in ); System.out.print( "Type dollar amount: " ); double dollars = in.nextDouble(); System.out.print( "Conversion rate: " ); double rate = in.nextDouble(); System.out.print( "Pesos:" ); System.out.println( dollars*rate ); }

Practice Programming Problem ► Write a program that reads one name from console and says hello to it. ► Sample Input Justin Bieber ► Sample Output Hello, Justin Bieber!