More on iterations using

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
CS110 Programming Language I
Java Programming Strings Chapter 7.
Hand Crafting your own program By Eric Davis for CS103.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
String and Scanner CS 21a: Introduction to Computing I First Semester,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
CS 160 – Summer 16 Exam 1 Prep.
Chapter 2 Clarifications
CSC111 Quick Revision.
Strings CSE 1310 – Introduction to Computers and Programming
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Objects.
Chapter 2 Elementary Programming
Building Java Programs
Conditional Execution
Lecture 2: Data Types, Variables, Operators, and Expressions
Building Java Programs Chapter 4.3
Chapter 5: Control Structures II
Introduction to Methods in java
Repetition.
Something about Java Introduction to Problem Solving and Programming 1.
An Introduction to Java – Part I
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Java Language Basics.
Java so far Week 7.
CS18000: Problem Solving and Object-Oriented Programming
CIS 110: Introduction to Computer Programming
Module 4 Loops.
Lecture 10 Strings CSE /26/2018.
class PrintOnetoTen { public static void main(String args[]) {
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Arrays in Java.
Building Java Programs
Recursion Method calling itself (circular definition)
Question 1a) What is printed by the following Java program? int s;
Just Enough Java 17-May-19.
What is a String? String s = "compsci"; s c o m p s i
Presentation transcript:

More on iterations using String and Char More on iterations using

Outlines Strings String variables Concatenation length() charAt() Iteration examples using length() and CharAt()

Characters and Strings A String is a series of chars. We can treat the series as one unit There are methods in the Java API we can use on strings

String variables String variables are unlike other variables we have used in this class. String variables are called reference variables because they hold a reference to where the string is stored in memory. Example: String s = "A string of characters";

String concatenation Two Strings together using the + operator to form a new string. This is called concatenation. The following statements: String s1 = “Hello "; String s2 = “Lahcen "; String s3 = s1 + s2; assigns the String “Hello Lahcen" to the variable s3.

String's charAt() method String also has a method called charAt() which can be used to return any single character in a String. Note: The first letter in a string is considered to be in position zero. For example, if we have: String s = "Hello"; s.charAt (0) will return 'H' s.charAt (4) will return 'o' s.charAt (5) will return StringIndexOutOfBoundsException

String's length() method All Strings know their own length. To find out the length of a string, we use String's length() method. For example: String s = "Hello"; int length = s.length(); Will place the value 5 in variable length.

Example Using while of for loops to write a program CountChartA.java. The program asks the user to enter a string and prints the number character ‘a’ in the string.

Answer with for loop import java.util.*; public class CountchartA { public static void main(String[] args) Scanner in = new Scanner(System.in); String s = in.nextLine(); int count = 0; for (int i=0; i < s.length(); i++) if (s.charAt(i) == 'a') count++; } if (count>1) {System.out.println("there are " + count + " charater 'a'");} else {System.out.println("there is " + count +" charter 'a'");}

Answer with while loop import java.util.*; public class CountchartA { public static void main(String[] args) Scanner in = new Scanner(System.in); String s = in.nextLine(); int count = 0; int I =0; while(i < s.length();) if (s.charAt(i) == 'a') count++; } i++; if (count>1) {System.out.println("there are " + count + " charater 'a'");} else {System.out.println("there is " + count +" charter 'a'");}

Example Using while of for loops to write a program StringReverse.java. The program asks the user to enter a string prints the string in reverse order i.e. if the the use enters “Hello”. the programs prints “olleH”.

Answer with while loop import java.util.*; public class StringReverse { public static void main(String[] args) Scanner in = new Scanner(System.in); String s = in.nextLine(); String rs = ""; for (int i=0; i < s.length(); i++) rs = s.charAt(i)+ rs; } System.out.println(rs);