2

I am trying to make a gematria calculator in Java and to try to get the same results I am getting with python. How would I get so in java it could print the sum of the values from what the user inputs and get the same ultimate result as I would in python? Please note I'm a beginner at java so forgive me for any ignorance in this question.

I managed to make one in python and want to do something conceptually similar in java. In python, I had a dictionary with all the English letters and their numeric values.

Python code

    Letter_values = {" ":0, 
    `"A": 1,`   
    "B": 2,
    # goes through rest of alphabet.}
    New_total = []  
    def get_gematria():  
          word = input("What do you want the gematria of?")
          upper_word = [x.upper() for x in word]
          for i in upper_word:
          New_total.append(Gematria[i])
          print(F"The gematria of {word} is {sum(New_total)}.")
          New_total.clear
          get_gematria() 
    get_gematria() 

According to my understanding in Java, I would be doing a hashtable and I get how to write that out.

Java code

    package com.company;
    import java.util.*;
    public class Main {

           public static void main(String[] args)  {
             Hashtable<String, Integer> Letter_Values = new Hashtable<>();
             Letter_Values.put("A", 1);
             Letter_Values.put("B", 2);
            // Goes through rest of alphabet
             System.out.println("What do you want the gematria of?")
             Scanner i = new Scanner(System.in);
             String Gematria = i.next();
             System.out.println("The gematria of " + Gematria + " is " 
               + Letter_Values.get(Gematria))

So in the Java program if the input is "A" (without the quotes) for output I'll get:

"The gematria of A is 1."

and for input I put in "B" (without the quotes) for output I'll get:

"The gematria of B is 2."

However, if enter AB or AA as input, the output will be:

"The gematria of AB is null."

I also tried putting a for loop right here:

for (int j = 0; j <Gematria.length(); j++) {
    System.out.println("The gematria of " + Gematria + " is " 
      + Gematria_Values.get(Gematria));

So again if I put A for input I get the same thing as before:

"The gematria of A is 1."

However, if I put AA or any combination of letters I get.

"The gematria of AA is null."
"The gematria of AA is null."

I get

"The gematria of x is null."

According to the number of letters, I put in. So if I put in AAAA. Output:

"The gematria of AAAA is null." 4 Times

I think it made it clear what I get with my java code.

In the python program if I put in in aa or ab I get:

"The gematria of aa is 2."

"The gematria of ab is 3."

So it would be nice to convert the user input to uppercase but that's not the main thing I'm asking here.

My main question is: How do I get the same ultimate result in java as I do in python?

Meir
  • 37
  • 1
  • 7
  • Please review the question, the example and the problem is clear, but the question does not represent exactly the problem you have. I will increase the chances of getting a response. – David Leal Jul 20 '20 at 03:22

1 Answers1

2

Update your for loop from

for (int j = 0; j <Gematria.length(); j++) {
    System.out.println("The gematria of " + Gematria + " is " + Gematria_Values.get(Gematria));

to

int sum = 0;
for (int j = 0; j < Gematria.length(); j++) {
    sum+= Letter_Values.get(String.valueOf(Gematria.charAt(j)));
}
System.out.println(sum);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
  • Thanks so much but I did that and it says "Cannot resolve symbol 'sum' so could you please add a little more info. – Meir Jul 20 '20 at 03:01
  • I updated the code. you can print the sum using System.out.println(sum) after for loop – Harmandeep Singh Kalsi Jul 20 '20 at 03:04
  • Thanks but is there a way I could get this to really be like the python so it prints it out only one time? For example with the updated code I did AA and it printed "The gematria of AA is 1." then on the next line "The gematria of AA is 2." so while it does get the right thing it goes through each letter first. Thanks again. – Meir Jul 20 '20 at 03:11
  • Could you please add the updated code to the question ? So that i can have a look , what changes you did . You might be printing it in loop – Harmandeep Singh Kalsi Jul 20 '20 at 03:12
  • LOL I got it, I was running the print statement in the for loop, I just put it out of the for loop and now it works. Thanks for the help. – Meir Jul 20 '20 at 03:19
  • Great , it helped . I hope you wont mind accepting the answer and also upvoting it for better visibility – Harmandeep Singh Kalsi Jul 20 '20 at 03:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218178/discussion-between-django-and-harmandeep-singh-kalsi). – Meir Jul 20 '20 at 03:36