This chapter will cover the basics of String and how to use String to solve problems.
Let's import the necessary libraries for our code to run.
import java.util.*;
As is briefly covered in Chapter 1, data that do not belong to primitive data types are considered reference data. There are eight primitive data types, including:
String is considered a reference data type. As a result, the conventional way of initializing and declaring a string variable would be:
String s = new String("Hello world");
System.out.println(s); // Hello world
If you initialize a String variable without declaring it, it will default to null. Note that null stands for "having no value at all", which is different from an empty string "", which is a string with no characters in it.
String s;
System.out.println(s); // null
String s = new String("");
System.out.println(s); // noting to print - empty string
An initialized and declared string should be treated as a single immutable item. In other words, once a string is created, it can not be changed or partially updated. However, a string is also a sequence of characters (data type char). For instance, given a string "Java is fun.", the indexes corresponding to the characters are:
To access the characters in this string, you can use the method charAt:
String str = "Java is fun.";
System.out.println(str.charAt(8)); // f
Please note that charAt accepts an integer as the only parameter, which stands for the index of character you want to access. It returns a char at the specified index.
Please also note that the method charAt is accessed by concatenating the defined string variable named "str" and the method "charAt" with a dot. This is what we call the dot notation. Java uses dot notation to access methods and fields defined underneath a class. In our example, you can think that String is a class defined in Java library, and charAt is a public method defined underneath String class. To access charAt, you need to have an initialized and declared String variable (e.g., str) first. After that, you can access the method by saying str.charAt(...). The same can be said for "System.out.println()": System is a class in the java.lang package . The out is a static member of the System class, and is an instance of java.io.PrintStream . The println is a method of java.io.PrintStream (I certainly know this souds crazy).
The above code snippets are listed in the following code cell. You are welcome to try them out and change the parameter values, and you may need to comment out some lines to get a clear sense of the ones you want to study.
// section 1
String s = new String("Hello world");
System.out.println(s); // Hello world
// section 2
String s;
System.out.println(s); // null
s = new String("");
System.out.println(s); // noting to print - empty string
// section 3
String str = new String("Java is fun.");
System.out.println(str.charAt(8)); // f
System.out.println(str.charAt(0)); // J
Hello world null f J
Three most commonly used string methods include:
"length" method is used to get the length of a Java String. The string length method returns the number of characters written in the String. When you traverse a string character by character, this method is often used as the cap for the iterator:
String str = new String("Java is fun.");
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i)); // print characters line by line
}
System.out.println();
Please note that there is a bracket that follows ".length", which is different from many other linear data structures (e.g., array). This is an unfortunate inconsistency in Java. You will get used to it given some time.
"+" method is used to concatenate a string with another item of any data types and returns the concatenated item as another string. "+" is used a lot when you have dynamically changed stuff to print:
for (int i = 0; i < 3; i++) {
System.out.println("Output number: " + i); // Output number: x
}
System.out.println();
"equals" is used to check the value equivalence of two given strings. String is a reference data type. When you initialize and declare a reference object (e.g., String str), its variable name and the actual objects are stored in different places in the memory of a computer. They are connected through a link we call pointer. In the contexts of String (as a reference data type), "==" is used for reference comparison (whether they are the same objects), while .equals() is used for content comparison (whether two objects have the same content). For instance, you can make two string variables with exactly the same values, but they would actually be two different objects:
String s = new String("Hello world");
String t = new String("Hello world");
System.out.println(s == t); // return false
System.out.println(s.equals(t)); // return true
The above code snippets are listed in the following code cell. You are welcome to try them out and change the parameter values, and you may need to comment out some lines to get a clear sense of the ones you want to study.
// section 1
String str = new String("Java is fun.");
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
}
System.out.println();
// section 2
for (int i = 0; i < 3; i++) {
System.out.println("Output number: " + i); // Output number: x
}
System.out.println();
// section 3
String s = new String("Hello world");
String t = new String("Hello world");
System.out.println(s == t); // return false
System.out.println(s.equals(t)); // return true
J a v a i s f u n . Output number: 0 Output number: 1 Output number: 2 false true
You might notice that you can actually declare a string object without explicitly creating a new object:
String str = "Java is fun.";
This is called String Literals and special to Java. String literal works in a different way in terms of whether a new object will be created in the process. When you declare string like this, you are actually calling intern() method on String. In short, if you use String literals to decalre a string variable (e.g., str2 with value of "Hello"), Java will be checking if there's already a string variable with the same value (e.g., str1 with value of "Hello"). If there is, str2 will reference that string object and no new String object will be created. This has some impacts on equivalance checking:
// wihtout string literals
String s = new String("Hello world");
String t = new String("Hello world");
System.out.println(s == t); // return false
System.out.println(s.equals(t)); // return true
// wiht string literals
String s = "Hello world";
String t = "Hello world";
System.out.println(s == t); // return true
System.out.println(s.equals(t)); // return true
The above code snippets are listed in the following code cell. You are welcome to try them out and change the parameter values, and you may need to comment out some lines to get a clear sense of the ones you want to study.
// wihtout string literals
String s = new String("Hello world");
String t = new String("Hello world");
System.out.println(s == t); // return false
System.out.println(s.equals(t)); // return true
// wiht string literals
String s = "Hello world";
String t = "Hello world";
System.out.println(s == t); // return true
System.out.println(s.equals(t)); // return true
false true true true
Based on the above content and knowledge covered in lectures, you should be able to solve the following listed problems. Please note that you are strongly recommended to solve the problems by yourself first before you look at the provided solutions.
Suppose we have two Strings s and t given by a user, wher each String contains at least one character. Write a program that returns if s and t are equal (case sensitive). Please do not use equals or other eqivalence checking methods. You can safely assume that both s and t have been declared
The code skeleton has been provided and you have to use it.
public boolean checkEquals(String s, String t) {
// ...
}
public boolean checkEquals(String s, String t) {
//different length
if (s.length() != t.length()) return false;
//same length
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != t.charAt(i)) {
return false;
}
}
return true;
}
// test
System.out.println(checkEquals("Hello", "cat"));
System.out.println(checkEquals("Hello", "Hello"));
false true
Scanner is a class in java.util package for obtaining input either from a user or a file. It is frequently associated with String in terms of processing input from users. Scanner is a reference data type, so its initialization and declaration will be the same as other reference data types:
Scanner sc = new Scanner(System.in);
A scanner object can be used to obtain inputs of various data types, and has a set of corresponding methods to read different data types:
A common usage case of Scanner could be:
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String name = sc.nextLine();
System.out.println("Hello " + name);
An improved usage case could be:
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Hello " + name + ".");
System.out.println("Which date do you want to meet?");
int date = sc.nextInt();
System.out.println("Let's meet at the date of " + date + ".");
The above code snippets are listed in the following code cell. You are welcome to try them out and change the parameter values, and you may need to comment out some lines to get a clear sense of the ones you want to study.
// // case 1
// Scanner sc = new Scanner(System.in);
// System.out.println("What is your name?");
// String name = sc.nextLine();
// System.out.println("Hello " + name);
// case 2
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Hello " + name + ".");
System.out.println("Which date do you want to meet?");
int date = sc.nextInt();
System.out.println("Let's meet at the date of " + date + ".");
cat Hello cat. Which date do you want to meet? 15 Let's meet at the date of 15.
Based on the above content and knowledge covered in lectures, you should be able to solve the following listed problems. Please note that you are strongly recommended to solve the problems by yourself first before you look at the provided solutions.
Take a string input from the user. print the input sting in reverse order. For example, if s is "abcd", then "dcba" should be printed. If there was no input string, print out, "There was no input to reverse".
The code skeleton has been provided and you have to use it.
public void reversePrint() {
// ...
}
public void reversePrint() {
//friendly info for users
System.out.println("Please input your string");
//Scanner
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//no string to reverse
if (str == null || str.length() == 0) {
System.out.println("There was no input to reverse");
System.exit(0);
}
//there is a string to reverse
String output = "";
for (int i = str.length()-1; i >= 0; i--) {
output += str.charAt(i);
}
System.out.println(output);
}
// test
reversePrint();
Please input your string hello world dlrow olleh
Code a simple log-in system:
The code skeleton has been provided and you have to use it.
private String ps = "111";
public void logIn() {
// ...
}
private String ps = "111";
public void logIn() {
//define scanner and userPassword
Scanner sc = new Scanner(System.in);
String userPass;
// log in system start
while (true){
//ask password
System.out.println("What is your password");
userPass = sc.nextLine();
// check against ps
if (ps.equals(userPass)) {
break;
} else if (userPass.equals("quit")) {
System.out.println("As you wish");
System.exit(0);
} else {
System.out.println("Wrong password");
}
}
//welcome
System.out.println("Welcome, user");
}
// test
logIn();
What is your password 112 Wrong password What is your password 111 Welcome, user
Input: Sting s taken from users Output: The number of English lowercase letters in s
Examples:
Enter a String s: BanAnA
Output: BanAnA has 3 lowercase letters
Enter a String s: ABcdefGHi123;
Output: ABcdefGHi123 has 5 lowercase letters
The code skeleton has been provided and you have to use it.
public void countLowercase() {
// ...
}
public void countLowercase() {
//get the string from users
System.out.println("Please input a string");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
//count lowercase letters
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c) && Character.isLowerCase(c)) {
count += 1;
}
}
//print result
System.out.println(s + " has " + count + " lowercase letter(s).");
}
// test
countLowercase();
Please input a string ABcdefGHi123 ABcdefGHi123 has 5 lowercase letter(s).