Strings
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
May 10, 2025
magicWord
, that contains value "abracadabra"
.Sample Output
nextLine
-method offered by Scanner
.Scanner reader = new Scanner(System.in);
System.out.print("What's your name?");
// reading a line from the user and assigning it to the name variable
String name = reader.nextLine();
System.out.println(name);
Sample Output
+
-operator between two strings, you get a new string that’s a combination of those two strings.String greeting = "Hi ";
String name = "Lily";
String goodbye = " and see you later!";
String phrase = greeting + name + goodbye;
System.out.println(phrase);
Sample Output
==
.equals
-command, which is always appended to the end of the string that we want to compare.String text = "chocolate";
if (text.equals("coffee")) {
System.out.println("The text variable contains the text coffee.");
} else {
System.out.println("The text variable does not contain the text coffee.");
}
equals
command is always appended to the end of the string that we want to compare, “string variable dot equals some text”.split
-method of the String class.split
method returns an array of the resulting sub-parts.
and prints each part of the string on a new line.Sample Output
and then prints the pieces that contain av
, each on a new line.csv
) format, where commas are used to separate values.Scanner reader = new Scanner(System.in);
while (true) {
String input = reader.nextLine();
if (input.equals("")) {
break;
}
String[] pieces = input.split(",");
System.out.println("Name: " + pieces[0] + ", age: " + pieces[1]);
}
Sample Output
and prints the first part of the string.Sample Output
and prints the last part of the string.Sample Output
Polymorphous computations elaborate.
Real calculators honour.
Older desktops deliver.
Great mainframes link.
Reversed devices install.
Additional workstations modem.
Many microcomputers letter.
charAt
method.Integer.valueOf()
)Scanner reader = new Scanner(System.in);
int sum = 0;
int count = 0;
while (true) {
String input = reader.nextLine();
if (input.equals("")) {
break;
}
String[] parts = input.split(",");
sum = sum + Integer.valueOf(parts[1]);
count = count + 1;
}
if (count > 0) {
System.out.println("Age average: " + (1.0 * sum / count));
} else {
System.out.println("No input.");
}
Sample Output
Sample Output
length()
-method:String word = "equisterian";
int length = word.length();
System.out.println("The length of the word" + word + " is " + length);
Sample Output
Sample Output 1
Computer Language