Question

In: Computer Science

suppose we have string like jason-458164888 how do we check if the numbers are vaild where...

suppose we have string like jason-458164888
how do we check if the numbers are vaild where the first number should be 4 and length of 9.
use simple program.

Solutions

Expert Solution

// CheckNumbers.java

public class CheckNumbers {

        public static void main(String[] args) {
                
                String s = "jason-458164888";
                
                int countNumbers = 0;
                int firstNum = -1;
                
                int i =0;
                while(i<s.length()) {
                        
                        // checking if character is integer
                        boolean flag = Character.isDigit(s.charAt(i));
                        
                        // if true it is a integer
                        if(flag == true) {
                                // counting numbers
                                countNumbers++;
                                // updating first number 
                                if(firstNum == -1) {
                                        firstNum = Integer.parseInt(String.valueOf(s.charAt(i)));
                                }
                        }                       
                        i++;
                }
                
                // if first number is 4 and counting numbers is 9
                // then print YES else NO
                if(firstNum == 4 && countNumbers == 9 ) {
                        System.out.println("YES");
                }else {
                        System.out.println("NO");
                }
                
        }

}

Code :

Output :


Related Solutions

Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with...
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with different exponents? Q2: Answer the following questions: What binary operation can be used to set bits? What bit pattern should the mask have? What binary operation can be used to unset bits? What bit pattern should the mask have? What binary operation can be used to flip bits? What bit pattern should the mask have?
What is multicollinearity in regression analysis? Why do we check for this issue? How can we...
What is multicollinearity in regression analysis? Why do we check for this issue? How can we detect multicollinearity? When we suspect multicollinearity, what should we do about it?
Suppose you have a group of N numbers and would like to determine the kth largest....
Suppose you have a group of N numbers and would like to determine the kth largest. This is known as the selection problem. There are quite a few “obvious” solutions. One way to solve this problem would be to read the N numbers into an array, sort the array in decreasing order by some simple algorithm such as bubble sort, and then return the element in position k. A somewhat better algorithm might be to read the 1st k elements...
How do we check if the independent variables are statistically significant and contribute to the multiple...
How do we check if the independent variables are statistically significant and contribute to the multiple regression models?
Suppose we have an array A that contains a prime numbers from 2 to 200 in...
Suppose we have an array A that contains a prime numbers from 2 to 200 in sorted order. How many items of the array A would the binary search algorithm have to examine before concluding that 60 is not in the array A? 30 200 100 6 2- Suppose we have an array that contains 182 village name. These names are sorted alphabetically. How many names would binary search algorithm examine to locate a particular name in the array, in...
Radix sort was proposed for sorting numbers, but if we consider a number as a string...
Radix sort was proposed for sorting numbers, but if we consider a number as a string of digits, the algorithm can be considered as a string sorting algorithm. In this project you are asked to implement a radix sort algorithm forsorting strings in ascending order. The input to your algorithm should be a (multi)set S = [S1, S2, . . . , Sn] of strings each of which is of length m over the English alphabet [A…Z, a…z]. The output...
Describe life in the universe. What will it look like and how and where can we...
Describe life in the universe. What will it look like and how and where can we find it? This is a complex question with a complex answer, so be sure you consider both the solar system and the universe as a whole
How are we to understand the experience that many of us have had that we like...
How are we to understand the experience that many of us have had that we like someone when we are friends with them, and get along with them well, but when we get into a relationship with them we find we can't stand them and don't get along at all?
In Visual Basics 6.Two String returning functions to have nice looking output for numbers/money are? 7.Where...
In Visual Basics 6.Two String returning functions to have nice looking output for numbers/money are? 7.Where do we place a class-level variable declaration, and where is it accessible from? 8.Two forms of parameter passing: 9.FULLY define an array: 10.Show the VB code to declare an initialize an array called Ar1 of Integers to hold the value of 5, 10 and 15. 11. For Ar1above, Ar1.Count = ________ and legal index values are:_____ to ______. 12.Show the VB code to declare...
How do you see the world around you in a physical sense? Where do you numbers...
How do you see the world around you in a physical sense? Where do you numbers and units come into play for you on a daily ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT