Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings CIS 362. What is a string? In Visual Basic fullName.

Similar presentations


Presentation on theme: "Strings CIS 362. What is a string? In Visual Basic fullName."— Presentation transcript:

1 Strings CIS 362

2 What is a string? In Visual Basic fullName

3 In C++

4 In Java

5 public class StringTest1 { public static void main(String[] args) { String fullName = new String("John Smith"); System.out.println(fullName.toLowerCase()); System.out.println(fullName.toUpperCase()); System.out.println(fullName.charAt(5)); System.out.println(fullName.substring(5)); System.out.println(fullName.substring(3,6)); System.out.println(fullName.length()); }

6 public class StringTest2 { public static void main(String[] args) { String fullName = " John Smith "; System.out.println(fullName.trim().length()); System.out.println(fullName.length()); fullName = fullName.trim(); System.out.println(fullName.length()); int x = fullName.indexOf("S"); System.out.println(fullName.substring(x)); x = fullName.lastIndexOf("h"); System.out.println(fullName.substring(x)); }

7 public class StringTest3 { public static void main(String[] args) { String fullName = GetData.getString("Enter the name"); if (fullName.equals(“Joe")) { System.out.println("Hey - it's Joe Spludnik."); } else { System.out.println("Hello " + fullName); } Enter the name: Bill Hello Bill Enter the name: Joe Hey - it's Joe Spludnik Enter the name: JOE Hello JOE

8 public class StringTest4 { public static void main(String[] args) { String fullName = GetData.getString("Enter the name"); if (fullName.equalsIgnoreCase(“Joe")) { System.out.println("Hey - it's Joe Spludnik."); } else { System.out.println("Hello " + fullName); } Enter the name: JOE Hey - it's Joe Spludnik. Enter the name: joE Hey - it's Joe Spludnik.

9 How do we convert a number to a String? public class StringTest4 { public static void main(String[] args) { int oldBadge = GetData.getInteger("Enter the badge number"); String newBadge = "A" + String.valueOf(oldBadge * 10); System.out.println("The new badge number is " + newBadge); } Our company is creating new badges for employees. The new badge will contain the existing badge number (which is stored as an integer like 1234) that will now be proceeded with an "A" and appended with a zero. For example, the badge 1234 becomes A12340.

10 How do we convert a string to a number? public class StringTest5 { public static void main(String[] args) { String oldBadge = GetData.getString("Enter the badge number"); oldBadge = oldBadge.substring(1); int tempBadge = Integer.parseInt(oldBadge) / 5; String newBadge = "A" + String.valueOf(tempBadge); System.out.println("The new badge number is " + newBadge); } We made a mistake. All the number portions of the new badges must be divided by 5. Therefore, the badge A5000 should now become A1000.

11 import java.util.*; public class StringTest { public static void main(String[] args) { GetInput in = new GetInput(); String firstName=null; String middleName=null; String lastName=null; String name = in.getString("Enter name"); StringTokenizer fullname = new StringTokenizer(name); switch (fullname.countTokens()) { case 1: lastName = fullname.nextToken(); break; case 2: firstName = fullname.nextToken(); lastName = fullname.nextToken(); break; case 3: firstName = fullname.nextToken(); middleName = fullname.nextToken(); lastName = fullname.nextToken(); break; default: System.out.println("Invalid Name"); } Tokenizer

12 (continued) lastName = initialCaps(lastName); firstName = initialCaps(firstName); middleName = initialCaps(middleName); if (lastName != null) System.out.println("LAST: "+lastName); if (firstName != null) System.out.println("FIRST: "+firstName); if (middleName != null) System.out.println("MIDDLE: "+middleName); } public static String initialCaps(String data) { if (data != null) { data = data.toUpperCase(); data = data.charAt(0) + data.substring(1).toLowerCase(); } return data; }

13

14 Homework 3 Part 6 We will be writing a program that translates a phone number into the English equivalent English words for the digits. So, for example, the phone number 816-941-0430 would become: Enter phone number: 816-941-0430 Area code: eight one six Prefix: nine four one Suffix: zero four three zero Enter phone number: 800-73-DEVRY Area code: toll free eight zero zero Prefix: seven three Suffix: three three eight seven nine Toll free numbers begin with 800 or 888.

15 First of all, here's the test program public class TestPhone { public static void main(String[] args) { PhoneNumber number = new PhoneNumber(); number.setNumber(GetData.getString("Enter phone number")); System.out.println("Area Code: " + number.getAreaCode()); System.out.println("Prefix: " + number.getPrefix()); System.out.println("Suffix: " + number.getSuffix()); }

16 PhoneNumber class You will need to add four private attributes: number, areaCode, prefix, and suffix. All will be Strings. Add a setNumber method. Add a breakUpNumber method that will split the number up into its three components and place them in the respective attributes. Add three get methods for areaCode, prefix, and suffix. Each of these methods will call the convertString method and pass its respective attribute to convertString. Return the string that convertString returns to the calling program. getAreaCode should also add "toll free" to the beginning of the returned string if the areaCode is "888" or "800".

17 PhoneNumber class continued convertString will be passed a String containing either the areaCode, prefix or suffix. Determine the length of this String and loop through it character by character. Send each character to the convertNumber method (supplied) which will return a word containing the converted character. Build a return String made up of these words. convertNumber will be passed a character and convert it into a word. convertString will use this to convert a character such as "1" to the word "one", or the character "R" to the word "seven".

18 convertNumber method in PhoneNumber class public String convertNumber(char letter) { String word = ""; //word to be returned switch (letter) { case '1': word = "one"; break; case '2': case 'A': case 'B': case 'C': word = "two"; break; case '3': case 'D': case 'E': case 'F': word = "three"; break; case '4': case 'G': case 'H': case 'I': word = "four"; break; case '5': case 'J': case 'K': case 'L': word = "five"; break; case '6': case 'M': case 'N': case 'O': word = "six"; break; case '7': case 'P': case 'Q': case 'R': case 'S': word = "seven"; break; case '8': case 'T': case 'U': case 'V': word = "eight"; break; case '9': case 'W': case 'X': case 'Y': case 'Z': word = "nine"; break; case '0': word = "zero"; break; } return word; }

19 UML for Homework 3 Part 6


Download ppt "Strings CIS 362. What is a string? In Visual Basic fullName."

Similar presentations


Ads by Google