CS 145 Lecture 7 – String Methods
Agenda
- what ?s
- right and wrong
- the String model
- String problems
TODO
- Finish homework 1. Dealing with setup issues, computer failures, and life’s troubles is part of homework. Only code submitted to Bitbucket will be graded.
Note
Writing code is much more enjoyable when you start to recognize the patterns that govern its structure. So, today we start with a little exercise I call Right and Wrong. Following are a set of problems that may or may not violate some of these patterns. Which are right and which are wrong?
String s = 'Coma sus ranas primero.'; System.out.print(s + " " + s);
String Guild = user.getGuild(); Guild = "Guild: " + Guild;
Scanner in = Scanner(System.in); int nDependents = in.nextInt();
String s1 = ""; int i; String s2 = s1 + i;
int a = Scanner.nextInt(); int b = Scanner.nextInt(); System.out.println(a / b);
Scanner parser = new Scanner("12 percent"); double percent = parser.nextDouble()
From here, go on to discuss the model of the String
class. We’ll see how it truly is a compound object made of primitives, and we’ll see how we can reach inside of it and get out the individual char
s that comprise it.
Encapsulating compound data is only half the beauty of an object. The other half is the set of operations that we can perform on the object—through its methods. Let’s have a look at many of the String
methods by solving some problems:
- You ask the user for a credit card number. Print it back to the console with all but the last four digits as asterisks.
- Students and teachers get a discount at your website. How can you tell if someone’s a student or teacher?
- You ask 112 people for their UWEC username—and username only. Many of them append @uwec.edu anyway. How can you clean up their input?
- You always send emails and say there’s an attachment, but you forgot to actually attach anything. How can you fix this problem?
- You’ve got the dimensions of the images your camera takes into a string of the form WIDTHxHEIGHT, where WIDTH and HEIGHT are actual numbers. How many megapixels is your camera?
- You have the city, state ZIP line of an address and need to make sure the state is properly capitalized. How do you do it?
How do we know how what the String
class can do for us? By consulting the API.
Code
CreditCard.java
package lecture0918;
import java.util.Scanner;
public class CreditCard {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your CC#: ");
String number = in.nextLine();
String masked = "************" + number.substring(12);
System.out.println(masked);
}
}
EduDiscount.java
package lecture0918;
import java.util.Scanner;
public class EduDiscount {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your email address: ");
String email = in.nextLine();
System.out.println("Edu? " + email.endsWith(".edu"));
}
}
RemoveDomain.java
package lecture0918;
import java.util.Scanner;
public class RemoveDomain {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your email address: ");
String email = in.nextLine();
int indexOfAt = email.indexOf('@');
String username = email.substring(0, indexOfAt);
System.out.println("Username: " + username);
}
}
Haiku
For i to team.length
Gimme a team.charAt(i)
In the cheerleading book