Search code examples
javaarrays2d

Find Numerology number in Java


So the question is:
Write a program to find the numerological value for a given name.
Note: Store the numerological number and the corresponding character in a 2-D array(2*26). Always the given name should be in capital case ,else the name is not valid. Check for the valid name,if the name is invalid print the message "Invalid name".There should not be any space in the name provided.

For example:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 8 3 5 1 1 2 3 4 5 7 8 1 2 3 4 6 6 6 5 1 7

Sample Input 1:
Enter your name:
SUDHA

Sample Output 1:
Your numerology no is:19

Sample Input 2:
Enter your name:
kiran

Sample Output 2:
Invalid name

Sample Input 3:
Enter your name:
ANI34

Sample Output 3:
Invalid name

"I am using an int 2d array to store value."

  int[][] arr = {{65,1},{66,2},{67,3},{68,4},{69,5},{70,8},

                {71,3},{72,5},{73,1},{74,1},{75,2},{76,3},

                {77,4},{78,5},{79,7},{80,8},{81,1},{82,2},

                {83,3},{84,4},{85,6},{86,6},{87,6},{88,5},{89,1},{90,7}};

But now i am confused how to extract character ASCII value and get the result.
Can anyone help me ?
thanks in advance


Solution

  • You can try :-


         int arr[][]={{65,1},{66,2},{67,3},{68,4},{69,5},{70,8},{71,3},{72,5},{73,1},{74,1},{75,2},{76,3},{77,4},{78,5},{79,7},{80,8},{81,1},{82,2},{83,3},{84,4},{85,6},{86,6},{87,6},{88,5},{89,1},{90,7}};
      
    

    System.out.println("Enter your name:");


         String name=sc.nextLine();
            String pattern="[A-Z]+";
             if(name.matches(pattern)){
                int score=0;
                for(int i=0;i<name.length();i++){
                    char ch=name.charAt(i);
                    score+=arr[(int)ch -65][1];
                }
                System.out.println("Your numerology no is:"+score);
            }
            else System.out.println("Invalid name");