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,

Slides:



Advertisements
Similar presentations
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

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.
Java Programming Strings Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
Chapter 2: Java Fundamentals Input and Output statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
Chapter Chapter 2 Getting Started with Java.
Chapter 2 - Introduction to Java Applications
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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!");
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
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.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
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.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Dialog Boxes.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Chapter 2 Basic Computation
2.5 Another Java Application: Adding Integers
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 2: Basic Elements of Java
Fundamentals 2.
Unit 1: Intro Lesson 3: Input.
Introduction to Java Programming
Chapter 2: Java Fundamentals
Unit 1: Intro Lesson 4: Output.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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, length, and indexOf. We will also introduce a string operation called concatenation.

String Indexing The position, or index, of the first character is 0.

charAt(i) tells the character at position i char a = text.charAt(0); char b = text.charAt(4); char c = text.charAt(7); char d = text.charAt(8); ‘E’‘E’ ‘e’‘e’ error ‘o’‘o’

Definition: substring Assume str is a String object and properly initialized to a string, then str.substring( i, j ) will return a new string by extracting characters of str from position i to j-1. String word = text.substring(1,4); word is “spr”

Examples: substring String a = text.substring(6,8); String b = text.substring(0,8); String c = text.substring(1,5); String d = text.substring(3,3); String e = text.substring(4,2); “so” “Espresso” “spre” error “”

Definition: length Assume str is a String object and properly initialized to a string. str.length( ) will return the number of characters in str. If str is “programming”, then str.length( ) will return 11 because there are 11 characters in it. The original string str remains unchanged.

Examples: length String str1, str2, str3, str4; str1 = “Hello” ; str2 = “Java” ; str3 = “” ; //empty string str4 = “ “ ; //one space int a = str1.length( ); int b = str2.length( ); int c = str3.length( ); int d = str4.length( );

Definition: indexOf Assume str and substr are String objects and properly initialized. str.indexOf( substr ) will return the first position substr occurs in str. If str is “programming” and substr is “gram”, then str.indexOf(substr ) will return 3 because the position of the first character of substr in str is 3. If substr does not occur in str, then –1 is returned. The search is case-sensitive.

Examples: indexOf String str; str = “I Love Java and Java loves me.” ; int a = str.indexOf( “J” ); int b = str.indexOf( “love” ); int c = str. indexOf( “ove” ); int d = str. indexOf( “Me” );

Examples: indexOf String str; str = “I Love Java and Java loves me.” ; int e = str.indexOf( “J”,8 ); int f = str.lastIndexOf(“J”); 16

Definition: concatenation Assume str1 and str2 are String objects and properly initialized. str1 + str2 will return a new string that is a concatenation of two strings. If str1 is “pro” and str2 is “gram”, then str1 + str2 will return “program”. Notice that this is an operator and not a method of the String class. The strings str1 and str2 remains the same.

Examples: concatenation String str1, str2; str1 = “Jon” ; str2 = “Java” ; String a = str1 + str2; String b = str1 + “ “ + str2; String c = str2 + “, “ + str1; String d = “Are you “+str1+“?”; “JonJava” “Java, Jon” “Are you Jon?”

What do you think it does? String str = “Hey there”; String str1 = str.toLowerCase(); //”hey there” int ind = str.indexOf(“e”,4); // 6 int ind = str.lastIndexOf(“e”); // 8 String str2 = str.substring(4); // “there”

String Equality String a = “cat”; String b = “cat”; boolean c = (a == b); Is c true or false?

String Equality String a = “cat”; String b = “cat”; boolean c = (a == b); Is c true or false? a -> c a t a and b are not the same b -> c a tobject, so the answer is false

String Equality String a = “cat”; String b = a; boolean c = (a == b); Is c true or false? a -> c a t a and b are the same b object, so the answer is true, but usually this isn’t what we have.

String Equality String a = “cat”; String b = “cat”; boolean c = a.equals(b); Use the “equals” behavior, which means the two strings are equivalent. The value of c is true.

Input and Output Two ways to input and output values Console input and output (like System.out) JOptionPanes (a.k.a. pop-up windows)

Standard Output Window A sample standard output window for displaying multiple lines of text. The exact style of standard output window depends on the Java tool you use.

The print Method We use the print method to output a value to the standard output window. The print method will continue printing from the end of the currently displayed output. Example System.out.print( “Hello, Dr. Caffeine.” );

The println Method We use println instead of print to skip a line. int x = 123, y = x + x; System.out.println( "Hello, Dr. Caffeine.“ ); System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ );

Standard Input The technique of using System.in to input data is called standard input. To input primitive data values, we use the Scanner class (from Java 5.0). Need to: import java.util.Scanner; Scanner scanner = new Scanner(System.in); int num = scanner.nextInt();

MethodExample nextDouble( )double d =scanner.nextDouble( ); nextInt( )int i = scanner.nextInt( ); next() String str = scanner.next(); nextLine()String wholeLine = scanner.nextLine(); Common Scanner Methods:

Static Objects Some objects can be used without being created via “new.” Use them directly:. ();

JOptionPane A static object (don’t use new) Must import javax.swing.JOptionPane to use. Using showMessageDialog of the JOptionPane class is a simple way to display a result of a computation to the user. JOptionPane.showMessageDialog(null, “I Love Java”); This dialog will appear at the center of the screen.

Displaying Multiple Lines of Text We can display multiple lines of text by separating lines with a new line marker \n. JOptionPane.showMessageDialog(null, “one\ntwo\nthree” );

JOptionPane for Input Using showInputDialog of the JOptionPane class is a simple way to input a string. String name; name = JOptionPane.showInputDialog (null, “What is your name?”); This dialog will appear at the center of the screen ready to accept an input.

Type Mismatch Suppose we want to input an age. Will this work? int age; age = JOptionPane.showInputDialog( null, “Enter your age”); No. String value cannot be assigned directly to an int variable. It can’t even be cast into an int.

Type Conversion Wrapper classes are used to perform necessary type conversions, such as converting a String object to a numerical value. String inputStr = JOptionPane.showInputDialog( null, “Enter your height”); double height = Double.parseDouble(inputStr);

Type Conversion Works for ints too… String inputStr = JOptionPane.showInputDialog( null, “Enter your age”); int age = Integer.parseInt(inputStr);

How do you remember it all? You don’t! API: Application programming interface Check it out...