So I'm fairly new to programming, and I'm working on a simple practice program that finds a letters order in the alphabet. This should have been pretty easy...but for some reason I get a StringIndexOutOfBoundsException when I add a while loop. So the program does do what it should the first time...but will not allow me to test again with out re-running the program. I tested the while loop with nothing but a simple print statement inside and it worked, so I'm confused as to why the while loop isn't working with my alphabet program.
Any help would be greatly appreciated thanks!
import java.io.*;
public class test {
public static void main(String[] args) throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
boolean again=true;
String response;
while (again)
{
System.out.println("Enter a letter to find it's order in the alphabet");
char theLetter = (char) in.read();
System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");
System.out.println("want to play again?");
response = in.readLine();
if (response.charAt(0)=='n')
{
again=false;
}
}
System.out.println("end program");
}
public static int convertLetter(char TheLetter)
{
//number value 'a'=97
//number value 'b'=98
//number value 'c'=99
//subtracting 'a' from any other number will reveal how many places away that number is from the start
//thus finding it's chronological place in the alphabet
int NumberValue= (int)TheLetter;
int a = 'a';
int CalulateOrder = (NumberValue - a) + 1;
return CalulateOrder;
}
}