I have this code which displays the letters of the alphabet. I am trying to get the JOptionPane to display the first six letters of the alphabet

However when I try and just display the 6 letters, it displays all of the alphabet letters first, which is not what I want it to do.
Here is my code

class Alphabet extends JFrame
{
	public static void main(String args[])
	{

		char[] 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'};


		StringBuilder builder = new StringBuilder(alphabet.length);

		for (int i = 0; i < alphabet.length; builder.append(alphabet[i++])) builder.append("\n");
		{
			JOptionPane.showMessageDialog(null, builder.toString(), "All the letters of the alphabet", JOptionPane.INFORMATION_MESSAGE);
		}


		for(int i = 0; i < alphabet.length-20; builder.append(alphabet[i++])) builder.append("\n");
		{
			JOptionPane.showMessageDialog(null, builder.toString(), "First 6 letters of the alphabet", JOptionPane.INFORMATION_MESSAGE);
		}


	}
}

Any help would be appreciated.

You need to delete the characters from your StringBuilder before your second loop or create a new one.

You could also consider deleting just the last 20 characters and doing away with the second loop altogether.

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.