My Simple Hashing Program
I have written a programme that accepts a name from the user, than extracts the first alphabet from the name and finds the hashcode for the name (the index where the name will be saved in an array).
Can someone comment on my coding, please? It works as i have put some mental constrains (like i wont type two name that has the first alphabets :D).
Can someone point out to me on how to make the programme into classes? I am just getting into doing java coding the OOP way.
Thank you.
Code:
import java.io.*; // to allow user to enter data using keyboard
class CMDA_Q2 {
public static void main (String[] args) throws IOException{
String ch, name;
int i=0;
do {
InputStreamReader rr = new InputStreamReader(System.in);
BufferedReader inp = new BufferedReader(rr);
System.out.println("Press [Enter] to exit the program");
System.out.print ("Please Enter a Name:");
name = inp.readLine();
char tem1;
int tem2 = 0;
int chValue;
// extracting the first alphabet from the input
tem1 = name.charAt(0);
// assigning a value for the alphabet
if (tem1 == 'a' || tem1 == 'A'){
tem2 = 1;
}
if (tem1 == 'b' || tem1 == 'B') {
tem2 =2;
}
if (tem1 == 'c' || tem1 == 'C'){
tem2 = 3;
}
if (tem1 == 'd' || tem1 == 'D') {
tem2 = 4;
}
if (tem1 == 'e' || tem1 == 'E') {
tem2 = 5;
}
if (tem1 == 'f' || tem1 == 'F'){
tem2 = 6;
}
if (tem1 == 'g' || tem1 == 'G') {
tem2 =7;
}
if (tem1 == 'h' || tem1 == 'H'){
tem2 = 8;
}
if (tem1 == 'i' || tem1 == 'I') {
tem2 = 9;
}
if (tem1 == 'j' || tem1 == 'J') {
tem2 = 10;
}
if (tem1 == 'k' || tem1 == 'K'){
tem2 = 11;
}
if (tem1 == 'l' || tem1 == 'L') {
tem2 =12;
}
if (tem1 == 'm' || tem1 == 'M'){
tem2 = 13;
}
if (tem1 == 'n' || tem1 == 'N') {
tem2 = 14;
}
if (tem1 == 'o' || tem1 == 'O') {
tem2 = 15;
}
if (tem1 == 'p' || tem1 == 'P'){
tem2 = 16;
}
if (tem1 == 'q' || tem1 == 'Q') {
tem2 =17;
}
if (tem1 == 'r' || tem1 == 'R'){
tem2 = 18;
}
if (tem1 == 's' || tem1 == 'S') {
tem2 = 19;
}
if (tem1 == 't' || tem1 == 'T') {
tem2 = 20;
}
if (tem1 == 'u' || tem1 == 'U'){
tem2 = 21;
}
if (tem1 == 'v' || tem1 == 'V') {
tem2 =22;
}
if (tem1 == 'w' || tem1 == 'W'){
tem2 = 23;
}
if (tem1 == 'x' || tem1 == 'X') {
tem2 = 24;
}
if (tem1 == 'y' || tem1 == 'Y') {
tem2 = 25;
}
if (tem1 == 'z' || tem1 == 'Z') {
tem2 = 26;
}
int hcode = 0;
// using modula method to find the unique key
hcode = tem2%27;
System.out.println("Hash Key Location in Array for " + name + " is at: "+hcode);
System.out.println(" ");
} while (name != " ");
System.out.println ("System Exited");
}
}