0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.