Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

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.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Computer Programming Lab(4).
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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[]
Introduction to Programming
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 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
(Dreaded) Quiz 2 Next Monday.
Introduction to Computer Science and Object-Oriented Programming
Introduction to programming in java
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.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CSCI 161 – Introduction to Programming I William Killian
Formatted Output (printf)
Exceptions and User Input Validation
Building Java Programs
String class.
First Programs CSE 1310 – Introduction to Computers and Programming
OUTPUT STATEMENTS GC 201.
INPUT STATEMENTS GC 201.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Common Mistakes with Functions
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
Introduction to Classes and Methods
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Strings CSE 1310 – Introduction to Computers and Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
First Programs CSE 1310 – Introduction to Computers and Programming
More on iterations using
Input and Formatted Output (printf)
Pre-AP® Computer Science Quiz
Presentation transcript:

Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

The String Type In the same way that int and double are designed to store numerical values, the String type is designed to store text. Text for strings must be enclosed in double quotes. Examples: String name = "George"; String phone_number = " "; 2

A Simple Program Using Strings 3 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Hi, my name is Java.\n"); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); System.out.printf("Hello %s %s, nice to meet you!\n", first_name, last_name); } Example Output: ???

A Simple Program Using Strings 4 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Hi, my name is Java.\n"); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); System.out.printf("Hello %s %s, nice to meet you!\n", first_name, last_name); } Example Output: Hi, my name is Java. What is your first name? Mary What is your last name? Smith Hello Mary Smith, nice to meet you!

String Input from the User As you see above, to read a string from user input, you use the Scanner.next() method. Note: although the code calls in.next(), the name of the method is Scanner.next(), because in is just an arbitrary variable name. 5 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Hi, my name is Java.\n"); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); System.out.printf("Hello %s %s, nice to meet you!\n", first_name, last_name); }

next and nextLine 6 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); System.out.printf("Hello %s\n", name); } Example Output: ???

next and nextLine What is wrong with the output here? 7 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); System.out.printf("Hello %s\n", name); } Example Output: What is your name? Mary Smith Hello Mary

next and nextLine What is wrong with the output here? The user types that the name is “Mary Smith”. However, the program does NOT print “Hello Mary Smith”. – It prints “Hello Mary”. 8 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); System.out.printf("Hello %s\n", name); } Example Output: What is your name? Mary Smith Hello Mary

next and nextLine The Scanner next method returns a single word that the user entered. It stops at the first space character. 9 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); System.out.printf("Hello %s\n", name); } Example Output: What is your name? Mary Smith Hello Mary

next and nextLine To get a single word from the user, use the Scanner next method. To get an entire line of text from the user, use the Scanner nextLine method. 10 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); System.out.printf("Hello %s\n", name); } Example Output: What is your name? Mary Smith Hello Mary

next and nextLine To get a single word from the user, use the Scanner next method. To get an entire line of text from the user, use the Scanner nextLine method. Here we changed in.next to in.nextLine, and now it works! 11 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.nextLine(); System.out.printf("Hello %s\n", name); } Example Output: What is your name? Mary Smith Hello Mary Smith

Length of a String To obtain the length of a string, we use the String.length() method. 12 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Hi, my name is Java.\n"); System.out.printf("What is your name? "); String name = in.next(); int length = name.length(); System.out.printf("Your name has %d letters!\n", length); } Example Output: Hi, my name is Java. What is your name? Vassilis ???

Length of a String To obtain the length of a string, we use the String.length() method. 13 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Hi, my name is Java.\n"); System.out.printf("What is your name? "); String name = in.next(); int length = name.length(); System.out.printf("Your name has %d letters!\n", length); } Example Output: Hi, my name is Java. What is your name? Vassilis Your name has 8 letters!

String Concatenation Using + string1 + string2 returns the result of putting those strings together. This is what we call "string concatenation". 14 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); String name = first_name + last_name; System.out.printf("Hello %s!\n", name); } Example Output: What is your first name? Mary What is your last name? Smith ???

String Concatenation Using + string1 + string2 returns the result of putting those strings together. This is what we call "string concatenation". 15 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); String name = first_name + last_name; System.out.printf("Hello %s!\n", name); } Example Output: What is your first name? Mary What is your last name? Smith Hello MarySmith!

String Concatenation Using + When you concatenate strings, make sure that you put spaces where they are needed. 16 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your first name? "); String first_name = in.next(); System.out.printf("What is your last name? "); String last_name = in.next(); String name = first_name + " " + last_name; System.out.printf("Hello %s!\n", name); } Example Output: What is your first name? Mary What is your last name? Smith Hello Mary Smith!

String Concatenation Using += The following two lines do the EXACT SAME THING: variable_name += value; variable_name = variable_name + value; 17 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String message = "Hello "; System.out.printf("What is your first name? "); String first_name = in.next(); message += first_name; System.out.printf("%s!\n", message); } Example Output: What is your first name? Mary ???

String Concatenation Using += The following two lines do the EXACT SAME THING: variable_name += value; variable_name = variable_name + value; 18 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String message = "Hello "; System.out.printf("What is your first name? "); String first_name = in.next(); message += first_name; System.out.printf("%s!\n", message); } Example Output: What is your first name? Mary Hello Mary!

Escape Sequences If you want to put a " character in a string: use \" If you want to put a \ character in a string: use \\ If you want to put a newline character in a string: use \n 19 public class example1 { public static void main(String[] args) { String a = "He said \"Hello\""; String b = "C:\\users\\jane\\note.txt"; String c = "*\n**\n***"; System.out.println(a); System.out.println(b); System.out.println(c); } Output: ???

Escape Sequences If you want to put a " character in a string: use \" If you want to put a \ character in a string: use \\ If you want to put a newline character in a string: use \n 20 public class example1 { public static void main(String[] args) { String a = "He said \"Hello\""; String b = "C:\\users\\jane\\note.txt"; String c = "*\n**\n***"; System.out.println(a); System.out.println(b); System.out.println(c); } Output: He said "Hello" C:\users\jane\note.txt * ** ***

Characters and Substrings The position of string characters are numbered starting from 0. To get the character at position p: use charAt(p); To get the substring from position s up to and not including position t, use substring(s, t) 21 public class example1 { public static void main(String[] args) { String a = "Hello, world!"; char first = a.charAt(0); char fifth = a.charAt(4); String sub = a.substring(2, 9); System.out.println(first); System.out.println(fifth); System.out.println(sub); } Output: ???

Characters and Substrings The position of string characters are numbered starting from 0. To get the character at position p: use charAt(p); To get the substring from position s up to and not including position t, use substring(s, t) 22 public class example1 { public static void main(String[] args) { String a = "Hello, world!"; char first = a.charAt(0); char fifth = a.charAt(4); String sub = a.substring(2, 9); System.out.println(first); System.out.println(fifth); System.out.println(sub); } Output: H o llo, wo

Printing Characters with printf To print a value of type char with System.out.printf, you should use %c. – %s will also work, but it is really meant to be used for strings. Do not use %s with characters. 23

Example: Printing Name Initial Write a program that: – Asks the user: What is your name? – Gets the name from user input. – Prints: Your initial is X where X is the first letter of the name that the user typed. 24

Example: Printing Name Initial 25 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("What is your name? "); String name = in.next(); char initial = name.charAt(0); System.out.printf("Your initial is %s\n", initial); } Example Output: What is your name? Mary Your initial is M Example Output: What is your name? John Your initial is J

Converting Numbers to Strings To convert an integer to a string, use the Integer.toString method. To convert a double to a string, use the Double.toString method. 26 public class example1 { public static void main(String[] args) { int a = 25; String s1 = Integer.toString(a); double b = 8.12; String s2 = Double.toString(b); System.out.printf("s1 = %s\n", s1); System.out.printf("s2 = %s\n", s2); } Output: ???

Converting Numbers to Strings To convert an integer to a string, use the Integer.toString method. To convert a double to a string, use the Double.toString method. 27 public class example1 { public static void main(String[] args) { int a = 25; String s1 = Integer.toString(a); double b = 8.12; String s2 = Double.toString(b); System.out.printf("s1 = %s\n", s1); System.out.printf("s2 = %s\n", s2); } Output: s1 = 25 s2 = 8.12

Converting to Upper and Lower Case To convert a string to upper case, use the toUpperCase method. To convert a string to lower case, use the toLowerCase method. 28 public class example1 { public static void main(String[] args) { String s1 = "January has 31 days and is COLD!!!"; String s2 = s1.toUpperCase(); System.out.printf("%s\n", s2); String s3 = s1.toLowerCase(); System.out.printf("%s\n", s3); } Output: ???

Converting to Upper and Lower Case To convert a string to upper case, use the toUpperCase method. To convert a string to lower case, use the toLowerCase method. 29 public class example1 { public static void main(String[] args) { String s1 = "January has 31 days and is COLD!!!"; String s2 = s1.toUpperCase(); System.out.printf("%s\n", s2); String s3 = s1.toLowerCase(); System.out.printf("%s\n", s3); } Output: JANUARY HAS 31 DAYS AND IS COLD!!! january has 31 days and is cold!!!

(Very) Common Mistake What is wrong with this code? What will it print? 30 public class example1 { public static void main(String[] args) { String s1 = "Hello"; s1.toUpperCase(); System.out.printf("%s\n", s1); s1.toLowerCase(); System.out.printf("%s\n", s1); } Output: ???

(Very) Common Mistake What is wrong with this code? What will it print? s1.toUpperCase DOES NOT CHANGE s1. – It creates a new string, that must be stored in a variable. – Same goes for s1.toLowerCase. 31 public class example1 { public static void main(String[] args) { String s1 = "Hello"; s1.toUpperCase(); System.out.printf("%s\n", s1); s1.toLowerCase(); System.out.printf("%s\n", s1); } Output: Hello

One Way to Fix the Mistake s1.toUpperCase DOES NOT CHANGE s1. – It creates a new string, that must be stored in a variable. – Same goes for s1.toLowerCase. In this example, we store the results of toUpperCase and toLowerCase into s2. 32 public class example1 { public static void main(String[] args) { String s1 = "Hello"; String s2 = s1.toUpperCase(); System.out.printf("%s\n", s2); s2 = s1.toLowerCase(); System.out.printf("%s\n", s2); } Output: ???

One Way to Fix the Mistake s1.toUpperCase DOES NOT CHANGE s1. – It creates a new string, that must be stored in a variable. – Same goes for s1.toLowerCase. In this example, we store the results of toUpperCase and toLowerCase into s2. 33 public class example1 { public static void main(String[] args) { String s1 = "Hello"; String s2 = s1.toUpperCase(); System.out.printf("%s\n", s2); s2 = s1.toLowerCase(); System.out.printf("%s\n", s2); } Output: HELLO hello

A Second Way to Fix the Mistake s1.toUpperCase DOES NOT CHANGE s1. – It creates a new string, that must be stored in a variable. – Same goes for s1.toLowerCase. In this example, we directly call s1.toUpperCase and s1.toLowerCase in the second argument of printf. 34 public class example1 { public static void main(String[] args) { String s1 = "Hello"; System.out.printf("%s\n", s1.toUpperCase()); System.out.printf("%s\n", s1.toLowerCase()); } Output: ???

A Second Way to Fix the Mistake s1.toUpperCase DOES NOT CHANGE s1. – It creates a new string, that must be stored in a variable. – Same goes for s1.toLowerCase. In this example, we directly call s1.toUpperCase and s1.toLowerCase in the second argument of printf. 35 public class example1 { public static void main(String[] args) { String s1 = "Hello"; System.out.printf("%s\n", s1.toUpperCase()); System.out.printf("%s\n", s1.toLowerCase()); } Output: HELLO hello