00:02
We want to write a program in java to enter a number using scanner class and check whether a number enter by the user is a single digit number, two digit number or a three digit number.
00:18
If the number enter is a single digit number, then check if the number is odd or even.
00:24
If the number enters a two digit number, then check if the number is more than 50 or not.
00:30
And if the number enter is a three digit number, then check.
00:33
If the number is divisible by 5 or not.
00:38
So here we have a java solution because we want to use the scanner object.
00:47
We also use the integer class where we have the parse -int method.
00:57
So we import these libraries, i .o .l .l .l .u .s .s .k .l .l .l.
01:05
Okay, so we create a class digits one, two, three, where we implement the solution in a main program, basically.
01:19
So we have a scanner object which we create here, here, this line, and we put a message on the screen for the user to know he or she got to enter a non -negative integer number.
01:38
Then we use the next line method of the scanner object to read the information the user put in into the program using the keyboard.
01:55
And we remember that all the information that is put into the program using the keyboard comes into the form of a string.
02:07
So that's why we have here a string object called mystring, where we capture the information the user put in.
02:17
So then we need a variable to hold a number where it will be analyzing.
02:23
The number is the variable is called number.
02:25
It's an integer variable and we initialize it to negative one.
02:31
Then because we want to know if the string that was entered by the user corresponds to an integer number.
02:41
Then we use the class integer and the method parsing applied to the string that was read by the user, my string, or that was entered by the user, and then we apply this method, which tries to convert the string into an integer number.
03:06
And that's why we put that into a try -catch block.
03:11
That is, we have to see if this method is successful or not.
03:18
So when it is not, the catch part of the block is executed.
03:25
For example, if the user enters a letter or even if the user enters a number, but there are some characters like dots, slashes, or all the characters that keep the method to convert the string to an integer number, the exception occurs, so the catch block is executed and then we put the message on the screen, error, string is not an integer number, and then we exit the execution of the program with the code of zero, meaning everything's okay, that is we ourselves stop the code.
04:08
But if the conversion is correct, the program is going to be executing line 26.
04:16
There is no instruction there, but the program keep going on.
04:21
And then we have an if else if else statement.
04:26
That's all the program does.
04:28
So first we check if the number enter.
04:31
Remember, now we are talking about the same variable number is going to hold the conversion of the string that is successful in this, at this point of the program.
04:43
And and then we are talking about here an integer variable.
04:48
Number is an integer variable.
04:50
It's not a string.
04:50
It's an integer number.
04:52
So we can compare that to integers.
04:55
In this case, we see if number is a negative number.
05:00
And that because we want to process only positive or non -negative instead, because we can have zero as a valid enter.
05:11
So we want to see if the number is not negative.
05:14
If it's negative, we put the message, the number is negative, please enter a negative integer, and we stop again with code x -code 0.
05:25
If the number is greater than or equal to 0, then we got to see if it's a single digit non -negative interior number.
05:32
That is equivalent to say that the number is less than 10.
05:38
Because if we are in this l -sif statement, we know this was false, that is the number is greater than or equal to.
05:46
0 and if it's less than 10 it means the number can be 0 1 up to 9 that is it's a single digit number in that case we put that message on screen single digit number and then we check this l if else if sorry if else we check if the number is even or is odd we know that the percentage um operator calculate the remainder of the division of the first argument to the second argument.
06:22
That is, number is divided by two.
06:24
And we know if we divide by two and the remainder is zero, the number is even.
06:30
So if the if condition is true, we put out the message, the number is even.
06:36
So we will have two messages in this case here...