Hi Everybody,
I've created an array to hold the alphabet . It uses the scanner class to ask the user for a position in the array and print that. I have the first part working however I need to use system.array copy to create a new array of size 27, puts a string containing a space (" ") at index 0 in the new array, and copies all the other letters to positions 1..26 of the new array. My code keeps giving me an array out of bounds exception. So, I was wondering if anybody can help me.
Thanks

public class Alphabet {

	// Fill an array with the alphabet
	private static String[] alphabet = { "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" };
	private static String[] alphabet2 = new String[alphabet.length];
	// Main
	public static void main(String[] args) {
		// Scanner
		Scanner console = new Scanner(System.in);
		// Asks the user for a position in the alphabet between 0 and 25
		System.out.println("Select the position to print by entering a value between 0 - 25");
		// Finds the index
		int theIndex = console.nextInt();
		// Prints the requested position and the letter at the position
		System.out.println("The letter of the alphabet at position "+ (theIndex) + " is : " + alphabet[theIndex]);
		System.arraycopy(alphabet, 0, alphabet2, 1, alphabet.length);
		System.out.println("The letter of the alphabet at position" + theIndex + "is :" + alphabet2[theIndex]);
		
	}
}

private static String[] alphabet2 = new String[alphabet.length];

You need to change the size of this array alphabet2 to alphabet.length + 1.Otherwise how can you accomodate the extra " " string?

yes abishek is correct...

private static String[] alphabet2 = new String[alphabet.length+1];

yes abishek is correct...

Thanks musthafa.aj, but kindly get my name correct... :)

sorry AbhikGhosh:)...

hasting make this mistake...

Thanks I don't know how I missed that.

hi vampgirl13 why dont u mark it solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.