Programming with Java String & Making Decision Lecture 05 First stage Software Engineering Dep. Saman M. Omer 2017-2018
Objectives By the end of this lecture you should be able to : Understand another data type of java, which is called String. Use a various String class methods. Explain the difference between sequence and selection. Use an if statement to make a single choice in a program. Use an if else statement to make a choice between two options in a program.
Outlines Introduction Creating String type Java String methods If statement If _ else Statement Examples
Introduction to String Generally, string is a sequence of characters. But In Java programming language, string is an object that represents a sequence of characters. Such as: a name, car registration number and any meaningless sequence of characters. java provides a String class that allows us to use and manipulate strings. String is actually a predefined class in the Java library just like the System class, and start with capital letter. The String type is not a primitive type. It is known as a reference type. String is an immutable object which means it is constant and can cannot be changed once it has been created.
Creating a String String: is a sequence of characters within double quotes. There are two ways to create a String in Java String literal String message = "Welcome to Java" ; Or String name ; name = " Welcome " ; Using new keyword String message = new String (" Welcome to Java ") ;
String type example
Java String Methods String class provides many useful methods, and listed some of them in the following. Method length() substring(int startindex) substring(int startindex,int endindex) charat(index) concat)another string) equals() equalsignorecase() touppercase() tolowercase() Description It returns count of total number of characters. it returns a part of the string. for given start index and end index it returns a character at that position in the string combines a specific string at the end of orignal string it compares the two given strings based on the content of the string It compares two string on the basis of content but it does not check the case like equals() It converts all the characters of the String to upper case. It converts all the characters of the String to lower case
Length of Strings length( ) method is used to return the number of characters in the string, including spaces. It will count from position zero, till \0 (excluded). Ex: int numchars; String street = Koya University / Koya ; numchars = street.length( ); Length is = 23
Length of Strings Example
Extracting Substrings We can extract substrings from larger ones. Using the following methods o o substring [start). substring [start, end). The index of the first character is zero, it append a \0 to all strings. The substring method take two parameters, they indicate the start and end of the substring. The start point is included, but the end point is excluded. If a position out of possible range is called, then an error will occur.
Extracting Substrings Example
Concatenating Strings Combines String with another string or other data types for printing. Other primitive data types like: int, double, char, float,... can easily concatenate with strings, and act as one string.. the + symbol is used for concatenation
Concatenating Strings method The String class includes a method for concatenating two strings.
Extracting Individual Characters of a String charat() method, it returns the character at the specified index in the string and store inside char type. Note that indexing start from zero. The first character in a string is at index 0.
String Equality The method equals( ) is used for determining the equality between two different strings. The method compares the two given strings on the basis of content of the string. The method will return true if strings are equal or all the characters are matched, and false if they are not equal.
String Equality equalsignorecase(): This method compares two string on the basis of content but it does not check the case like equals() method. In this method, if the characters match, it returns true else false.
touppercase & tolowercase methods tolowercase(): method converts all the characters of the String to lower case (Small Letter). touppercase() : method converts all the characters of the String to upper case (Capital Letter).
Making Choices Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Making Choices Java programming language provides following types of decision making statements. No. 1 2 3 4 Statement & Description if statement An if statement consists of a boolean expression followed by one or more statements. If.. else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. nested if statement You can use one if or else if statement inside another if or else if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values.
The if statement The if statement, provides decision. An if statement consists of a Boolean expression followed by statement(s). Syntax: If (Boolean_expression) Statement(s) ; Note: Statements will execute if the Boolean expression is true. Otherwise, the statement is skipped.
Note
Example
The if else statement The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. Syntax : if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Note : Only one of the branches will execute, true-case or false-case.
Example
Example Write a Java program to read a value from keyboard, and then determine the result of the following mathematical function:
Example : Write a Java program to read a value from keyboard, and then determine whether the number is an odd or even number.
Example : Write a Java program to read a character from position five of any string that entered and then determine if its digit or not? H e l l o 1 2 3 0 1 2 3 4 5 6 7