Strings CSE 1310 – Introduction to Computers and Programming

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
03 Data types1June Data types CE : Fundamental Programming Techniques.
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 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
PHP : Hypertext Preprocessor
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.
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.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
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.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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,
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.
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Computer Science and Object-Oriented Programming
Strings CSE 1310 – Introduction to Computers and Programming
Lecture 4 – Scanner & Style
Strings CSE 1310 – Introduction to Computers and Programming
CSCI 161 – Introduction to Programming I William Killian
Formatted Output (printf)
Chapter 2 Elementary Programming
Exceptions and User Input Validation
Chapter Goals To understand integer and floating-point numbers
USING ECLIPSE TO CREATE HELLO WORLD
Maha AlSaif Maryam AlQattan
Java Concepts Chapter 2 - Using Objects
Computer Programming Methodology Input and While Loop
First Programs CSE 1310 – Introduction to Computers and Programming
Something about Java Introduction to Problem Solving and Programming 1.
OUTPUT STATEMENTS GC 201.
Common Mistakes with Functions
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
An Introduction to Java – Part I, language basics
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
Consider the following code:
More on iterations using
Input and Formatted Output (printf)
Presentation transcript:

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

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 = "310-123-987";

A Simple Program Using Strings 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 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); } 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.

Length of a String 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! To obtain the length of a string, we use the String.length() method.

String Concatenation Using + 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! string1 + string2 returns the result of putting those strings together. This is what we call "string concatenation".

String Concatenation Using + 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! When you concatenate strings, make sure that you put spaces where they are needed.

String Concatenation Using += 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! The following two lines do the EXACT SAME THING: variable_name += value; variable_name = variable_name + value;

Escape Sequences If you want to put a " character in a string: use \" If you want to put a newline character in a string: use \n 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); }

Escape Sequences If you want to put a " character in a string: use \" If you want to put a newline character in a string: use \n 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) 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); }

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) 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 can use either %s or %c (they both work).

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.

Example: Printing Name Initial 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