How to assign a value to each character in a Java string

I use it txtwith processing software and assign it to an array, for example, for example:

dog
cat
wolf...

But then I need to have a = 1, b=2, c=3etc .... to make a difference, with the result that complete words will be formed, and so I can find which word has the greatest value.

I could make a ton if, but 26 of them would be too much and unnecessary. So, in this case, what would be the best way to do this?

+4
source share
3 answers

char . , . , Java char UTF-16 , , 16 , short. . ( , ,) .

, "a = 1", "b = 2" .., int v = inputChar - 'a' + 1 , "A" "a".

+3

. loadStrings() , , for:

String lines[] = loadStrings("list.txt");
println("there are " + lines.length + " lines");
for (int i = 0 ; i < lines.length; i++) {
  println(lines[i]);
}
+1

a = 1, b = 2, c = 3 .. If, 26

char ch = ...
int a = ch & 0x1f;

int a = ch % 32;

int a = ch - 'a' + 1;

. ASCII 'a' to 'z' .

http://www.asciitable.com/index/asciifull.gif

, "a" 97, 3- 32 ASCII, , 32 , 31 A A 1 B B 2. , if.

0

Source: https://habr.com/ru/post/1624696/


All Articles