I created a hashing method to make a=1, b=2, c=3...etc. To me it doesn't seem too efficient. Does this look efficient or is there a recursive method or smaller method to do the same thing?

``````/**
* Changes String to int
* @param key: String
* @return (result % arraySize): int
*/
public int hashFunc (String key)
{
int result = 0;
String inputString = key.toString().toLowerCase();
// changes String to lowercase
char [] characters = inputString.toCharArray(); // makes String an Array
for (int i = 0; i < characters.length; i++) // loop
{
char currentChar = characters[i]; // gets current character

if (currentChar == 'a')
{ // if character is a
result += 1;
// get character numbers
}// end if
if (currentChar == 'b')
{ // if character is b
result += 2;
// get character numbers
}// end if
if (currentChar == 'c')
{ // if character is c
result += 3;
// get character numbers
}// end if
if (currentChar == 'd')
{ // if character isd
result += 4;
// get character numbers
}// end if
if (currentChar == 'e')
{ // if character is e
result += 5;
// get character numbers
}// end if
if (currentChar == 'f')
{ // if character is f
result += 6;
// get character numbers
}// end if
if (currentChar == 'g')
{ // if character is g
result += 7;
// get character numbers
}// end if
if (currentChar == 'h')
{ // if character is h
result += 8;
// get character numbers
}// end if
if (currentChar == 'i')
{ // if character is i
result += 9;
// get character numbers
}// end if
if (currentChar == 'j')
{ // if character is j
result += 10;
// get character numbers
}// end if
if (currentChar == 'k')
{ // if character is k
result += 11;
// get character numbers
}// end if
if (currentChar == 'l')
{ // if character is l
result += 12;
// get character numbers
}// end if
if (currentChar == 'm')
{ // if character is m
result += 13;
// get character numbers
}// end if
if (currentChar == 'n')
{ // if character is n
result += 14;
// get character numbers
}// end if
if (currentChar == 'o')
{ // if character is 0
result += 15;
// get character numbers
}// end if
if (currentChar == 'p')
{ // if character is p
result += 16;
// get character numbers
}// end if
if (currentChar == 'q')
{ // if character is q
result += 17;
// get character numbers
}// end if
if (currentChar == 'r')
{ // if character is r
result += 18;
// get character numbers
}// end if
if (currentChar == 's')
{ // if character is s
result += 19;
// get character numbers
}// end if
if (currentChar == 't')
{ // if character is t
result += 20;
// get character numbers
}// end if
if (currentChar == 'u')
{ // if character is u
result += 21;
// get character numbers
}// end if
if (currentChar == 'v')
{ // if character is v
result += 22;
// get character numbers
}// end if
if (currentChar == 'w')
{ // if character is w
result += 23;
// get character numbers
}// end if
if (currentChar == 'x')
{ // if character is x
result += 24;
// get character numbers
}// end if
if (currentChar == 'y')
{ // if character is y
result += 25;
// get character numbers
}// end if
if (currentChar == 'z')
{ // if character is z
result += 26;
// get character numbers
}// end if
}// end for
return (result % arraySize); // returns Hashed values
} // end hashFunc1()
``````

Replace the list of if statements with a statement that returns the value for the character.
Since the values of the chars are contiguous: 'b' = 'a' + 1 and you can do arithmetic with char variables,
consider these: 'b' - 'a' = 1 or 'z' - 'a' = 25

wouldn't you need the if statement though? It's meant for an entire string. Like if someone were to type Delaware it would turn that String into 11 since my hashtable is 29.

I feel there has to be a recursive way to do this

Do what? Seems like a loop is easier. By using arithmetic, lines 16 to 145 can be replaced by one statement.

I guess I don't understand. It has a loop. You think I should add another for it? I don't think I know how that would work because each time it makes a char an int it has to add with the previous values it found as well as obtain the value from the current char.
I could initially make chars the values 1,2,3,4..., but I don't know how that would work and still get the correct result. I just tried...

``````public int hashFunc (String key)
{
int result = 0;
String inputString = key.toString().toLowerCase();
// changes String to lowercase
char [] characters = inputString.toCharArray(); // makes String an Array
char a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,o=15,p=16,
q=17,r=18,s=19,t=20,u=21,v=22,w=23,x=24,y=25,z=26;
for (int index = 0; index < characters.length; index++) // loop
{
result+=characters[index];
}// end for
return (result % arraySize); // returns Hashed values
} // end hashFunc1()
``````

This is getting 10 for A it needs to be 1. I could subtract 1, but I want to understand why it is making A as a 10.

not subtract 1 but 9

If 'A' is 1, then is 'B' 2 and 'C' 3 etc to 'Z' 26?

'B' - 'A' + 1 = 2
'C' - 'A' + 1 = 3
'Z' - 'A' + 1 = 26