I'm writing a program that takes an input string and gets the length and/or gets the number of vowels and consonants in the string. I've gotten it working except for consideration of spaces (which are allowed) and error checking for input other than letters. Here is what I've got so far. ANY suggestions would be greatly appreciated. Thank You

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class LetterStringManip extends JFrame
{//declaring variables
     private JLabel stringOfLettersL, lengthL, lengthRL, vowelsL, vowelsRL, consonantsL, consonantsRL;
	  private JTextField stringOfLettersTF;
	  private JButton lengthB,vowelB, quitB;
	  private LengthButtonHandler lbHandler;
	  private VowelButtonHandler vbHandler;
	  private QuitButtonHandler qbHandler;
	  private static final int WIDTH = 750;
	  private static final int HEIGHT = 150;
	  
	  public LetterStringManip()
	  {// setting up GUI window with buttons
	      stringOfLettersL = new JLabel("Enter a String of Letters", SwingConstants.CENTER);
			lengthL = new JLabel("Length of String: ", SwingConstants.CENTER);
			lengthRL = new JLabel("", SwingConstants.LEFT);
			vowelsL = new JLabel("Number of Vowels: ", SwingConstants.CENTER);
			vowelsRL = new JLabel("", SwingConstants.LEFT);
			consonantsL = new JLabel("Number of Consonats: ", SwingConstants.CENTER);
			consonantsRL = new JLabel("", SwingConstants.LEFT);
			
			stringOfLettersTF = new JTextField(15);
		
			lengthB = new JButton("Length");
			lbHandler = new LengthButtonHandler();
			lengthB.addActionListener(lbHandler);
			
			vowelB = new JButton("Vowels");
			vbHandler = new VowelButtonHandler();
			vowelB.addActionListener(vbHandler);
			
			quitB = new JButton("Quit");
			qbHandler = new QuitButtonHandler();
			quitB.addActionListener(qbHandler);
			
			setTitle("String Of Letters");//Window title
			
			Container pane = getContentPane();
			
			pane.setLayout(new GridLayout(3,4)); //Window layout
			
			pane.add(stringOfLettersL);
			pane.add(stringOfLettersTF);
			pane.add(lengthL);
			pane.add(lengthRL);
			pane.add(vowelsL);
			pane.add(vowelsRL);
			pane.add(consonantsL);
			pane.add(consonantsRL);
			pane.add(lengthB);
			pane.add(vowelB);
			pane.add(quitB);
	
			
			setSize(WIDTH, HEIGHT);
			setVisible(true);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			
	  }//closes public LetterStringManip()
	  
	  private class LengthButtonHandler implements ActionListener
	  {
	      public void actionPerformed(ActionEvent e)//Length Button is clicked, getLength() method is performed
			{
				
			   getLength();
				  
			}//closes public void actionPerformed(ActionEvent e)
			
	  }//closes private class LengthButtonHandler implements ActionListener
	  
	  private class VowelButtonHandler implements ActionListener
	  {
	      public void actionPerformed(ActionEvent e)//Vowels Button is clicked, getVowels() method is performed
			{
				
			   getVowels();
				  
			}//closes public void actionPerformed(ActionEvent e)
			
	  }//closes private class VowelButtonHandler implements ActionListener
	  
	  private class QuitButtonHandler implements ActionListener//Quit Buttton is pressed
	  {
	      public void actionPerformed(ActionEvent e)
			{
			    System.exit(0);
				 
			}//closes public void actionPerformed(ActionEvent e)
			
	  }//closes private class QuitButtonHandler implements ActionListener
	  
	  		public void getLength()//gets the length of the string
			{ 
			    String letString = (stringOfLettersTF.getText());
			    String lengthString;
             int length;
				 length = letString.length();
				 lengthString = Integer.toString(length);
				 lengthRL.setText(lengthString);
			 }//closes public void getLength()
	  
	  		public void getVowels()//gets the number of vowels in the string
			{     
             String letString = (stringOfLettersTF.getText());
			    String vowString;
				 String conString;
             int countVow = 0;
				 int countCon = 0; 
				 int i;
				 int length = letString.length();
				 char[] letter = new char[length];
					  
             for (i = 0; i < letString.length(); i++)
				 {
                 letter[i] = letString.charAt(i);
                 if (letter[i]=='a' || letter[i]=='e' || letter[i]=='i' || letter[i]=='o' || letter[i]=='u')
                 countVow++;
				 }//closes for

				 countCon = letString.length() - countVow;
             vowString = Integer.toString(countVow);
				 conString = Integer.toString(countCon);
				 vowelsRL.setText(vowString); 
				 consonantsRL.setText(conString);

			 }//closes public void getVowels()
			 
	  public static void main(String[] args)
	  {
	      LetterStringManip stringObject = new LetterStringManip();
			
	  }// closes public static void main(String[] args)
	  
}//closes public class LetterStringManip extends JFrame

Recommended Answers

All 2 Replies

What's the problem ?
See help on code tags, would be more readable if you add =java.

No problem, figured it out, thanks though

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.