Find Numerology number in Java

Viewed 3893

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

3 Answers

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");
 

Hey you can use a hash map This is what I did

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner (System.in);
        HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();//HashMap
        hmap.put('A',1); ///VALUES
        hmap.put('B',2);
        hmap.put('C',3);
        hmap.put('D',4);
        hmap.put('E',5);
        hmap.put('F',8);
        hmap.put('G',3);
        hmap.put('H',5);
        hmap.put('I',1);
        hmap.put('J',1);
        hmap.put('K',2);
        hmap.put('L',3);
        hmap.put('M',4);
        hmap.put('N',5);
        hmap.put('O',7);
        hmap.put('P',8);
        hmap.put('Q',1);
        hmap.put('R',2);
        hmap.put('S',3);
        hmap.put('T',4);
        hmap.put('U',6);
        hmap.put('V',6);
        hmap.put('W',6);
        hmap.put('X',5);
        hmap.put('Y',1);
        hmap.put('Z',7);
        
        System.out.println("Enter Name");
        String str = sc.next();
        /**
         * if(!str.isUpperCase())
         * {
         *   System.out.println("INVALID NAME");
         *    System.exit(0);
         * }
         */
         
        str=str.toUpperCase();
        long sum=0;
        for(int i=0;i<str.length();i++)
        {
            char ch = str.charAt(i);
            sum+=hmap.get(ch);
        }
        System.out.println(sum);
    }
}

`

Since you mentioned in your comment that letters have random numerical values, and you need to have only uppercase letters, you can create a map in which only uppercase letters as key and their random values as value. After that you can check your each word's each char with the map to make sure you have only uppercase letters. Below code will print the results, so you can store them in array or any other container as you wish.

public static void main(String[] args) {
    // map which contains only upper case letters with random values
    Map<Character, Integer> map = new HashMap<>();
    for (int i = 0; i < 26; ++i){
        // upper case A ascii value is 65, we start from 65
        map.put((char)(65 + i), (int) (Math.random() * (10)));
    }

    // input values
    String[] inputs = {"SUDHA", "kiran", "ANI34"};
    boolean valid;
    int sum;

    for (int j = 0; j < inputs.length; ++j){
        sum = 0;
        valid = true;
        for (int i = 0; i < inputs[j].length(); ++i){
            // if char is not in map, it means that it is not uppercase letter
            if (!map.containsKey(inputs[j].charAt(i))){
                System.out.println("Sample output " + (j + 1) + ": Invalid Name: " + inputs[j]);
                valid = false;
                break;
            }else{
                sum += map.get(inputs[j].charAt(i));
            }
        }
        if (valid){
            System.out.println("Sample output " + (j + 1) + ": Your numerology no is: " + sum);
        }
    }
}
Related