I am trying to convert the word that the user inputs into binary. The program runs correctly, but after the user inputs his/her word there is an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26 at home.main(home.java:84)
Here is my code:
import java.util.Scanner;
import java.util.Vector;
public class home
{
public static void main(String[] args)
{
Scanner getWord = new Scanner(System.in);
char[] alphabet = new char[]
{
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
};
String[] Binary = new String[]
{
"00001 ",
"00010 ",
"00011 ",
"00100 ",
"00101 ",
"00110 ",
"00111 ",
"01000 ",
"01001 ",
"01010 ",
"01011 ",
"01100 ",
"01101 ",
"01110 ",
"01111 ",
"10000 ",
"10001 ",
"10010 ",
"10011 ",
"10100 ",
"10101 ",
"10110 ",
"10111 ",
"11000 ",
"11001 ",
"11010 ",
};
String word;
System.out.println("Type in what you want to convert into binary: (to exit type in 'quit')");
while(true)
{
Vector<String> wordBin = new Vector<String>();
word = getWord.next();
if(word == "quit")
{
break;
}
for(int a = 0; a < word.length(); a++)
{
for(int b = 0; b < 27; b++)
{
if(word.charAt(a) == alphabet[b])
{
wordBin.addElement(Binary[b]);
}
}
}
System.out.println();
System.out.println("That in binary is: ");
System.out.println();
for(int c = 0; c < wordBin.size(); c++)
{
System.out.println(wordBin.get(c));
}
System.out.println();
System.out.println("What is the next word that you would like to type in: ");
}
System.out.println();
System.out.println("Hava a nice day");
}
}
I am using Eclipse Mars.1 to run the program. Any help is appreciated.