Strings in Java
Activity Outcomes:
This lab teaches you the following topics:
- How to define strings
- How to use strings in problem solving
- Using predefined string methods
- Passing strings to methods
Introduction:
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings [Oracle]. String literals are collection of characters enclosed in double quotation marks. In many programming assignments, you will be required to accept or process alphabetic or alphanumeric data. For this purpose you may use strings to get the required results.
The String Class
Java provides a built-in String class which provides a number of useful methods which may be used to process strings.
Constructing a String
Strings can be constructed in a number of ways as below.
String s=new String(“COMSATS”);
Or
String s=”COMSATS”
We may also take strings as input from user at runtime.
For this purpose we may use next() or nextLine() methods of Scanner class as discussed in the later activities of this lab session.
Concatenating Strings
Anything added with a string is called string concatenation.
For example if we add “abc” with “xyz”, then the result will be “abcxyz”.
Similarly if we add “street” with a numeric integer 1 then the result will be “street1” as below.
“street”+1 will result “street1”.
Strings can also be concatenated by using built-in concat() method as below.
“string1”.concat(“string2”)
String Built-in Methods
Strings class has a number of useful methods. Some of them are as below.
“string1”.equals(“string2”)
returns true if “string1” and “string2” have same value otherwise false
“string1”.equalsIgnoreCase(“string2”)
returns true if “string1” and “string2” have same value otherwise false ignoring the case
“string1”.compareTo(“string2”)
Returns o, positive or negative integer value depending on that whether string1 is equal to, greater than or less than string2.
“string1”.compareToIgnaoreCase(“string2”)
Returns 0, positive or negative integer value depending on that whether string1 is equal to, greater than or less than string2 ignoring case.
“string”.length()
Returns length of the string.
“string”.charAt(index)
Returns the character at specified index.
“string”.concat(“string2”)
Concatenates the two strings.
“Pakistan”.substring(0,3)
Returns a 3-charcter long substring starting at index 0.
“Pakistan”.indexOf(“k”)
Returns index of “k” in “Pakistan”, i.e. 2. If the string to find is not present in the string then returns a negative integer.
“Pakistan”.lastIndexOf(“a”)
Returns last index of “a” in “Pakistan”, i.e. 6 in this example.
Lab Activities:
Activity 1:
Write a Java program to accept two strings from user and check that whether they are same or not.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Here is another output if the strings are different.
If strings are same but one is given in lowercase and the other in uppercase, then the output will be as below.
Activity 2:
Rewrite the last code to accept two strings from user and compare with each other ignoring case.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 3:
Write a java program to accept two strings from user and display the smaller string.
Kindly note that smaller does not mean here that it will have less number of characters in it but you need to compare the strings on the basis of ASCII character. For example,
“ahsan” is less than “zia” as ASCII code of “a” is less than that of “z”.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 4:
Write a Java program to accept names of 5 students and sort in ascending order.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Activity 5:
Write Java program to accept names of 5 students from user in an array. Pass this array to a method. The method will sort the names in descending order.
Solution:
Create a new Java file and type the following code.
Run the code by pressing F5.
You will get the following output.
Home Activities:
Activity 1:
Write a Java program to accept names and marks of 10 students. Display names of student having maximum marks.
Activity 2:
Write a Java program to accept names and marks of 10 students. Sort this data according to names in ascending order.
Activity 3:
Write a Java method to accept marks of a student between 0-100 and return the grade according to the following criteria.
0 – 49 F
50 – 60 E
61 – 70 D
71 – 80 C
81 – 90 B
91 – 100 A