7. The TextField class
Programs often require the input of data. In Java one way to do this is by means of an object of the class TextField. We could declare such an object like this:
TextField input;Just like some other objects we learned to know, objects of the TextField class must be initialized in the init() method with:
input = new TextField ("type your name here");In the first example the text type your name here is put in the textfield. In the second line, a textfield is created wide enough for twenty characters (on average). Again - you might be surprised about its name: textField, but as we saw before, Java is case-sensitive, so the object textField is different from the class TextField.
Just like buttons, textfields must be added in the init() method with:
add (textField);Of course, such textfields are useless unless we have access to the text that has been typed into it. We can copy this text to a string using the method getText(), which is part of the TextField class:
String text = input.getText();We can also put a string in a textfield with the method setText(), or we can put an "empty" string in it, to clear the textfield):
input.setText ("All data have been input");If the textfield in the last line contained any text, this text will actually be removed since it is replaced by an "empty string".
Suppose we want to write an applet which shows the number of characters that an input text consists of. The String class has a method length(), to find out the number of characters in a string. This method can be used like this:
String s = "Be happy";The text "Be happy" contains seven letters, but after this operation the variable wordLength will have the value 8. Spaces are also characters. If you quickly want to check the value of wordLength, you could write the following line in your program:
System.out.println (wordLength);This will display the value of the variable wordLength, but not in the applet window. The text will appear on the text screen (the DOS-screen). If you want to find errors in your program, the method System.out.println can be a handy tool. It enables you to quickly show the values of variables you don't trust.
Finally the complete applet:
// An applet which shows the number of input characters
Different types of numbers
So far, we have come across no more than one type of numbers, i.e. the type int, which is used for whole numbers. If we would try to assign a fractional number to an int variable, the compiler will issue an error warning and won't compile our applet.
int number = 0.75; // So this is wrong !!!
In order to store fractional numbers, we have the type double. So the following line is a correct statement:
double number = 0.75;
When compiling a program, Java checks as much as possible if we don't try to store numbers of the double type into an int. Consequently, the following two lines won't be accepted by the Java-compiler.
double number = 9;
int i = number;
Here, nothing seems to be wrong. After all, 9 is an integer, so why wouldn't it fit into in int i?
But it does not matter whether the number accidentally fits. A double can contain more information than an int. Java won't allow a large number to be stored into a type meant for smaller numbers. After all, if we pour the contents of a large bucket into a smaller one, things can go wrong!
If we really want the content of a double to be stored into a variable of type int, we must use a cast, like this:
double number = 9;
int i = (int)number;
To store fractional numbers that are less accurate, we might use the type float. As this type does have some limitations, you'd better not use it too often.
It may be useful to draw the attention to another type which is used for whole numbers, the type long. This type can handle extremely large numbers (from -9223372036854775808 to 9223372036854775807), while the type int does not go further than -2147483648 to 2147483647.
But to be honest, in 25 years of programming I've never needed the type long for any practical application.
The type char and the method charAt()
A string consists of characters. Java has a type of variable that can contain one character. It is the char type. The content of a char (the actual symbol) must be surrounded by single quotation marks, for instance like this:
char hash = '#';As you can see, the type char can contain characters from other collections than the Roman alphabet. This is due to the fact that the char type in Java works with Unicode. In Unicode, each character consists of two bytes (65536 combinations), unlike ASCII, a system in which every character is no greater than 1 byte (256 combinations).
Occasionally, it may be necessary to handle separate characters of a string. This can be done with the charAt(n) method (from the String class). This method returns the nth character of a string. You should realize that Java starts to count at 0 (zero). So if you want to put the first character of a string in a variable, you could do it like this:
String progLang = "Java";After this operation, the variable character will contain the letter J.
If we want a part of a Java program to be executed a definite number of times, it can usually be done best with a so-called "for-loop". A "for-loop" consists of the word for, followed by (in parentheses) three "fields", separated by semicolons. The fields are:
1. starting value and sometimes also declaration of the control-variableSuppose we want to display the numbers 0 through 8 on the text screen, we could do that with:
for (int i=0; i<9; i++)Note that the second line is indented because its operation depends on the first line.
We could show the even numbers 2 through 10, for instance like this:
for (int i=2; i<=10; i+=2)The for-loop makes it possible to process a string character by character. If we want to remove the spaces from a text, we could do it like this:
String text = "text with spaces";After this, the string s will contain the text textwithspaces (so without spaces).
We could make an applet that converts the letters of a typed text to upper case. We could do that by making use of the fact that the unicode numbers of upper case letters are 32 lower than those of lower case letters. For instance, the unicode number of 'A' is 65, whereas the letter 'a' has number 97.
// Text is converted to upper case.
As you can see, the actual conversion is done by this line:
k = (char)(k-32);The expression (char) is called a cast. A cast (also referred to as 'typecast') enables us to convert one type of variable into another. We could, for instance, show the the unicode of the letter 'A' with:
System.out.println ((int)'A');Here we use the cast (int) to show the 'A' in the form of a number. Sometimes a cast is needed in Java, especially if information could otherwise get lost, like in the following lines:
float a = 10, b = 5;If we put these lines in an applet, the compiler will report an error. It is true, 10/5 equals 2, but a division of two variables of the float type could also result in a number that is not an integer (a floating-point number). If we put that in a variable of the int type, the fraction part (the part after the dot) might get lost. We must make it clear to the compiler that this is no mistake, which we do by casting the result to int:
int c = (int)(a/b);Division of two ints
The cast (int) used above would not be necessary if both a and b were of the int type. If we divide two variables of the int type, Java always calculates the result as an int, as in the following divisions:
int a = 7, b = 3;You would probably expect a value like 2.3333333 as result. But no, the resulting value of c is 2. Division of two int-variables is calculated in Java as an int. Fraction parts (if any) are not calculated.
Finally, we should mention that the String class has the standard method toUpperCase() to convert a string to upper case:
String capitals = lowerCaseString.toUpperCase();
Please write an applet in which the numbers 1 through 10 are shown in the form of a table (of one column) in the applet window.
Please write an applet in which the the numbers 1 through 80 are shown as a table (with several columns) with the help of a for-loop.
Please write an applet in which a Dutch postal code (consisting of 4 numerical digits and two letters) can be input. The applet will then inform you whether the code is correct or not.
Please write an applet in which a sentence can be typed into a textfield. After clicking on a button the sentence will be shown again. But then each r has been changed into l and each R into an L.
to chapter 8
Main Menu Java Tutorial
To home page
(c) Thomas J.H. Luif