Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Principles of Programming B

Notes
(PPB115D/PPG115D/
PPBFD/PPGF15D)

Compiled by
V. Booi

All rights reserved. Apart from any fair dealing for the purpose of research
criticism or review as permitted under Copyright Act, no part of this
document may be reproduced or transmitted in any other form or by any
means, electronic
Learning outcome:or mechanical,
C.2. includingclasses
Using predefined photocopy and recording, without
in programs.
permission in writing from the publisher.
Learning Outcome C: Using characters to manipulate data in programs

In this learning unit, you are will be introduced to Java’s character methods.
The techniques discussed in this learning unit are for validating program input, displaying
information to users and other text based processing.

Characters
 Characters are the fundamental building block of every java source program.
 Every program contain a sequence of characters that are grouped together
meaningfully and they interpreted by the java compiler.
 A program may contain character literals.
 A character literal is an integer value represented as a character in single quotes.
 This integer value is referred to as an ASCII code for a character.

Figure 1 [Sample code]

Figure 2 [Sample output]

NB: a character data type is a primitive data type.


Commonly used methods of the Character class

How to use a Character class method:


 Open the Character class in the API. The methods section will show you all the
methods of the class Character. The first column indicates the return type of the
method. The second column shows the name of the method with the parameters it
needs and also gives you a short description of the method.

Below are character methods you should understand to manipulate characters:

1. isDigit () this method of the Character class and is used to determine if a character is a digit.

Figure 3[Sample Code]

Figure 4[Sample Input and Output]


2. isLetter() method of the Character class is used to determine if a character is a letter.

Figure 5[Sample Code]

Figure 6[Sample Input and Output]


3. isLetterOrDigit() method of the Character class is used to determine if a character is a letter
or digit.

Figure 7[Sample Code]

Figure 8[Sample Input and Output]


4. isLowerCase() method of the Character class is used to determine if a character is lower
case.

Figure 9[Sample Code]

Figure 10[Sample input and output]


5. isUpperCase() method of the Character class is used to determine if a character is upper case.

Figure 11[Sample Code]

Figure 12[Sample Output]


6. isWhitespace() method of the Character class is used to determine if a character is a
whitespace.

Figure 13[Sample Code]

Figure 14[Sample Output]


7.
toLowerCase() method of the Character class is used to convert a character to lower case.
toUpperCase() method of the Character class is used to convert a character to upper case.

Figure 15[Sample Code]

Figure 16[Sample input and output]


Exercises
1. Write a program that will allow a user to enter a number of characters. The program must
then calculate how many characters and how many digits are entered by the user and an
asterisk (*) must be used to terminate the process.

2. Write a program that will allow a user to enter a number of characters and an asterisk ( *) to
terminate the program. The program must determine in a static method called letterDigit()
if the character entered is a character or a digit and it must return a true if it is a letter or
digit or a false if it is not any of the above.

You might also like