Presentation is loading. Please wait.

Presentation is loading. Please wait.

Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle."— Presentation transcript:

1 Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle

2 2 The Character (char) Type The character ( char ) type in Java is considered a numeric type because it is stored internally as a number (technically, a non-negative integer). The character ( char ) type in Java is considered a numeric type because it is stored internally as a number (technically, a non-negative integer). Do you remember our earlier discussion about ASCII/Unicode values for letters and digits? Do you remember our earlier discussion about ASCII/Unicode values for letters and digits? If not, I have again provided the table of characters and their numeric values on the next slide (A similar table appears on p. 100 in your book.). If not, I have again provided the table of characters and their numeric values on the next slide (A similar table appears on p. 100 in your book.).

3 3 ASCII/Unicode Values for Letter and Digit Characters Character ASCII/Unicode Value '0' through '9' 48 through 57 'A' through 'Z' 65 through 90 'a' through 'z' 97 through 122

4 4 Converting Between Characters and Numbers Since the character ( char ) type is stored internally as a number, it is easy to convert between characters and numbers in Java. Since the character ( char ) type is stored internally as a number, it is easy to convert between characters and numbers in Java. For the same reason, it is possible to perform mathematical operations on characters in Java (“ditto,” for the C and C++ programming languages). For the same reason, it is possible to perform mathematical operations on characters in Java (“ditto,” for the C and C++ programming languages).

5 5 Converting Between Characters and Numbers Number to character Number to character –(char)(48) = 0... Why? Character to number Character to number –(int)('x') = 120... Why? Character math Character math –'a' - 'A' = 32... Why? Combining both Combining both –(char)('E' + ' ') = e... Why?

6 6 Sample Programs Now let’s look at some sample programs that demonstrate mathematical/conversion operations on characters: Now let’s look at some sample programs that demonstrate mathematical/conversion operations on characters: –MathOpsOnChars.java MathOpsOnChars.java –CopyTextFile2.java CopyTextFile2.java

7 7 Sample Programs Now let’s look at a demo program relevant to our current Mastermind assignment: Now let’s look at a demo program relevant to our current Mastermind assignment: –HeadsOrTails.java HeadsOrTails.java


Download ppt "Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle."

Similar presentations


Ads by Google