00:01
We want to write a java program called count vowels digits, which prompt the user for a string, counts the number of vowels, which can be lowercase or uppercase, and the digits contain in the string and prints the counts and percentages rounded to two decimal places.
00:26
That is, it prints out the number of digits and the number of digits, of vowels with its with their respective percentages in the final printout so here i have put the program the job program made to solve this problem and as you know in this case we have only one class and you get to name the file the same as a specified here get to be called count vowels digits this name here so that's the name of the class public class count vowels digits here and that's the most of the name of the file so the file is called counts vowel digits with the dot java extension as usual okay so here we are going to use the input output library java .io that we are importing everything from that library the length library to use some character features and strings we use a scanner to read the stream from the prompt and the local is i'm going to use myself in this case because i'm using spanish characters in my window system and to force java to print out decimals with point separator.
02:22
But if you're in the us or uk, you don't have to use that locale that i use here.
02:29
I'll print that in a minute.
02:35
So we import all those libraries we're going to use.
02:38
We declare our public class count foul digits.
02:41
And inside that we have our main public study function with the argument by default given by jab, jabba.
02:53
And now here we are going to count vowels and digits, so we need two variables.
03:00
I've called them count vowels here and count digits.
03:10
They are integer values, variables, and they contain zero at the star, so we are going to increment each time we count a vowel or a digit.
03:21
Next we find the object scanner to read the stream from screen.
03:29
I call the variable my scanner and i create a new object that type and i specify here is to enter information.
03:44
Then i ask the user to enter the string, leaving a line in the middle.
03:55
And now i read the line from screen is the...
03:59
From the scanner object you have the next line method to read string there are all the methods in the scanner object to read other type of variables of input but next line refers to the string of the type of variable and then i calculate the length of the string which i need to not only to go through the string but to calculate the percentages at the end so i put that in a variable called my string length of type integer then we have a cycle or loop going through all the characters in the string that we read from screen that is the variable my scanner or sorry the variable my string my string have the string we're going to use to count the vowels and digits.
05:07
So my string, as you see here, we go through i equals zero, less than the length of the string, we read from the screen, and we increment one unit every time we made a loop.
05:26
And here we have the lowercase method of the character class, and that we used to lowercase, the character of the string at position i.
05:43
That is, in fact, the character at position i, which is the one we use in any iteration of this cycle, is calculated by this method of the string object.
05:58
That is my string, that, the method is sharp at, and we give the position i.
06:04
So this is given us the character of the string, we read from screen at position i.
06:17
And that's what we want.
06:18
We want to analyze each of the characters of the string we read from screen.
06:25
And we do that by pulling out or taking that character using the method chart at of the string object.
06:35
My string is the string, the string object we read from screen.
06:40
And so we apply the chart add method of that variable my string at position i so that my string char ad i represents the character at position i of the string my string we read from screen but now we want to lowercase that and the reason why we do that is because if we have a vowel and we want to can't a vowel we don't want a lowercase or an uppercase vowel to be different.
07:12
We want it to be the same.
07:14
So we lowercase each time we read a character from the string and we lowercase it, we count the vowels in lowercase.
07:26
That way we are counting both lowercase and uppercase.
07:32
And that we do through, as i said, through the to lowercase method of the character object and that we put that in it because that's a character you put that in a chart type variable name current character and having that character which is lowercase when it corresponds to a letter if it's a number or another type of character lowercase do nothing to the character it only made and if and take an effect when the character is a letter, in that case it's lowercase the character.
08:16
If that character we read and lowercase is the a letter, or is the e letter, or is i, or is o, or is u, that is any of the vowels, we count a vowel, that is the variable count vowels is incremented by one.
08:39
Is not that's not the case that is a character is different from a vowel we get to verify if the character is a digit that's what we use an else if that is we are going to we are not in the case where we have a vowel that is the character is not a bubble and we want to verify now that the character is a digit in the case the is a digit, we count a digit with count digit plus plus as we did for the vowels.
09:18
But here we could have done the same thing i did it here up, that is, i could have said current character equal zero character, that is that, or current character equal one character and so on.
09:43
Of course, we will have a long list because we have 10 possible characters.
09:48
Corresponding to digits, but that will work perfectly.
09:54
But there is a good thing when we work with characters in java, and that's similar thing in c+ + and c, there is a automatic conversion when we do a comparison of this type with the operators greater than less than even equal.
10:17
There is an internal, let's say, transformation or casting of the value of the character to the ascii table.
10:31
That's a good thing to know.
10:34
And i'm using this here because it simplifies the problem in this part of the digits.
10:41
And the ascii table is a table of 128 characters, basic characters, that were the first one that were used when computers were constructed.
10:50
And those vexic character contains, among others, letters in lowercase, uppercase letters, digits and other characters.
11:03
So digits as well as lowercase variables and uppercase variables or letters are placed in specific zones of the asc table.
11:20
That is, for example, the digits are contiguous in the asky table.
11:27
There is, if zero is in, i don't know what position it is at this moment, but i know that the one character is just the next following the zero character.
11:40
The two characters the next following the one character.
11:43
They are placed one after the other in a zone of the aski table.
11:49
And that made that way in order to use this type of comparison.
11:56
That is when we say a character greater than character zero and then that same character less than or equal character nine, we are talking about the aski table.
12:08
And because they are contiguous on that table, then these two comparisons with the a and connector means being a digit.
12:19
That is between the first digit zero up to the last digit 9.
12:28
In the case of the vowels, we could not do this because the vowels are not contiguous.
12:34
The letters are contiguous, that is the a, b, c, up to the z letter.
12:39
If we want to know a character is lowercase letter, for example, we could say character greater than or equal to lower a and character less than, are equal to character z.
13:01
And with that, we are talking about a character that is a letter in lowercase.
13:07
And we can do the same with the overcase instead of the lowercase.
13:11
And we are talking about a letter in uppercase.
13:14
And that's a magic thing that happens because there is an automatic transformation or casting from the character value to the corresponding value on the ascii table.
13:27
I'm going to show you, in fact, the ascii table.
13:28
I'm going to show you, in fact, the askie table a moment for you to see what i'm talking about a little more specifically.
13:37
So here we have the basic askie table.
13:43
Let me put it a little bit...