D
D
DS_JS2020-12-25 12:50:11
Java
DS_JS, 2020-12-25 12:50:11

How to build application logic?

Good day, I just started learning programming and I have a small problem
we have a text input field enter a word and press the button and get a number
there is an alphabet in which each character is assigned its own number and the editText text input field the resulting word needs to be divided into separate characters and calculate the sum of all the digits belonging to these characters,
tell me how to correctly build the logic to
create each character as a separate variable and assign the desired number, or is it better to drive the alphabet into array A and create a separate array B with numbers and assign the desired number through index search?
I would like to emphasize that since I am a beginner and want to learn everything, please explain in more detail why and how best

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-12-25
Hasanly @azerphoenix

You can implement your task in different ways. If there are no restrictions, then you can look aside:
1) use 2 arrays. The first is char[] and the second is int[]. Further, as you mentioned, by index you get the desired value.
2) Either use Map. For example, its HashMap implementation.
3) Or, for example, in the apache commons lang3 package there is a Pair object. And you can use something like List<Pair<Character, Integer>>.
In any case, everything will come down to the following:
- get a word (string) and break it into characters.
- declare an int variable in which the values ​​of the letters will be summed up
- then, depending on the selected array or collection, iterate over it and, if the letter matches, get its numeric value and add it to the above-declared variable.
Something like this

R
Ruslan., 2020-12-26
@LaRN

You can get by with one array if you put its digit at the index equal to the character code.
For example, if your alphabet starts with Latin A, then you can count it as 0 and
fill the array like this
int [] code;
code[0]=1; // character A
code[1] =2; // character B
...
And look for the desired code like this:
code[(char) x - (char) 'A')]
x is a variable into which characters from the input string are sequentially read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question