I am trying to write a method that can take a starting lowercase letter and an ending lowercase letter and generate random letters in that range. Next, the random letters are assigned to the elements of an array of a size determined by the user. So far, my code seems to work as long as the starting character is 'a'. I can't figure out how to make it work when the starting character is not 'a'. I wrote this complete program to try to troubleshoot my method:
import java.util.Random;
import java.util.Scanner;
public class Add
{
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter array size: ");
int arraySize = scan.nextInt();
char charArray[] = new char[arraySize];
int fromChar;
int toChar;
do
{
System.out.print("Enter first character: ");
String line = scan.next();
fromChar = line.charAt(0);
System.out.print("Enter second character: ");
line = scan.next();
toChar = line.charAt(0);
Random generator = new Random();
for(int i = 0; i < arraySize; i++)
{
int randomInt = generator.nextInt(toChar - fromChar + 1) + 97;
//System.out.print(randomInt + " | "); //used to print unicode values
charArray[i] = (char)(randomInt);
}
System.out.println(charArray);
}
while(true);
} // end of main
}// end of class
Any help would be greatly appreciated!