How do you add up certain values in hashtable based off of user input?

早过忘川 提交于 2021-01-27 19:01:37

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/62987856/how-do-you-add-up-certain-values-in-hashtable-based-off-of-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!