Hello all,
I'm writing a program that gets an input string entered and I was wondering if there is an exception thrown so that the string is valid only if it is all letters. I can find exceptions thrown for only doubles, int.... as valid input but none for only letters. can anyone help me out?

I should further explain that the exception I need is for the getText method, thanks

Hmmm, I don't recall any methods for that particular issue. However, there is a Throwable class (wich is the superclass for all exceptions in the Java Language) and an Exception class. This means that you can create your own exceptions.

Maybe you should take into consideration that approach.

do your own input validation. Relying on Exceptions being thrown on bad input is usually not a good idea.

Even if you create your own Exception you will still need to implement some sort of validation in order to decide whether to throw the exception or not. So it up to you to decide, once you have made the validation, if it is necessary to throw an Exception or not.

That right there is where I am having trouble, I am not finding a way to validate input where only letters are allowed and spaces are ignored.
Here are the parts of my program that I'm working with:
Thanks

public void getLength()
			{ 
			    String letString = (stringOfLettersTF.getText());
			    String lengthString;
             int length;
				 length = letString.length();
				 lengthString = Integer.toString(length);
				 lengthRL.setText(lengthString);
			 }
	  
	  		public void getVowels()
			{     
             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++;
				 }

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

			 }

OK, I've got it to ignore the spaces when calculating the number of consonants and length of the string, but there has got to be a more efficient way to allow only letters as input than comparing every character to every uppper and lower case letter. if I'm understanding you correctly. This is how I've dealt with spaces:

public void getLength()
			{ 
			    String letString = (stringOfLettersTF.getText());
			    String lengthString;
				 int finalLength;
				 int countSpace = 0;
             int length = letString.length();
				 char[] letter = new char[length];
				 int i;
					  
             for (i = 0; i < letString.length(); i++)
				 {
                 letter[i] = letString.charAt(i);
                 if (letter[i]==' ')
                 countSpace++;
				 }
				 
				 finalLength = length - countSpace;
				 lengthString = Integer.toString(finalLength);
				 lengthRL.setText(lengthString);
				 
			 }
	  
	  		public void getVowels()
			{     
             String letString = (stringOfLettersTF.getText());
			    String vowString;
				 String conString;
             int countVow = 0;
				 int countCon = 0;
				 int countSpace = 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++;
					  if (letter[i]==' ')
                 countSpace++;
				 }

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

			 }

Hello all, I've got my data validated the way I want finally, but I'm having a hard time with this next part, I need the program to terminate after 3 instances of wrong input. I've tried sticking another if else loop with a counter but it's not happening for me. I'm really stumped this time. Any suggestions? This is the method I've written so far.

public void getLength()//gets the length of the string
			{ 
			    String letString = (stringOfLettersTF.getText()), lengthString;
                            int length = letString.length(), i;
		            char[] letter = new char[length];
					  
                            for (i = 0; i < letString.length(); i++)
		           {
                                letter[i] = letString.charAt(i);
				if ((Character.isLetter(letter[i]) == true) || (letter[i]==' '))
			       {			 
				    lengthString = Integer.toString(length);
				    vowelsRL.setText(" ");
			            consonantsRL.setText(" ");
				    lengthRL.setText(lengthString);
			        }//closes if
			        else
			       {
			           stringOfLettersTF.setText("ERROR");
                                   lengthRL.setText(" ");
			           vowelsRL.setText(" ");
				   consonantsRL.setText(" ");
				}//closes else
			   }//closes for
		 }

Here is my question now, I've got the input validated (I had already done it a different way, but thank you though). Now I need the program to terminate after pressing one of the buttons 3 times with invalid inputs. You can see how I've attempted it thus far. I'm not sure how to get a counter going outside of the button handler so that the counter doesn't reset everytime the button is pushed. Would this be easier using your suggested masked JFormattedTextField?

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

public class LetterStringManip extends JFrame
{
     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()
	  {
	      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");
			
			Container pane = getContentPane();
			
			pane.setLayout(new GridLayout(3,4)); 
			
			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);			
	  }
	  
	  private class LengthButtonHandler implements ActionListener
	  {
	      public void actionPerformed(ActionEvent e)
			{
				int errorCount = 0;
				boolean errorCheck = false;

				getLength();
				
				if (errorCheck = true)
				{
				    errorCount++;
				}
				while (errorCount > 1)
				{
				    System.exit(0);
				}		  
			}		
	  }
	  	  
	  private class VowelButtonHandler implements ActionListener
	  {
	      public void actionPerformed(ActionEvent e)
			{
				int errorCount = 0;
				boolean errorCheck;

				getVowels();
				
				if (errorCheck = true)
				{
				    errorCount++;
				}
				while (errorCount > 1)
				{
				    System.exit(0);
				}
			}		
	  }
	  
	  private class QuitButtonHandler implements ActionListener
	  {
	      public void actionPerformed(ActionEvent e)
			{
			    System.exit(0);				 
			}			
	  }
	  
	  		public boolean getLength()
			{ 
			    String letString = (stringOfLettersTF.getText()), lengthString;
             int length = letString.length(), i;
				 boolean errorCheck = false;
				 char[] letter = new char[length];
					  
             for (i = 0; i < letString.length(); i++)
				 {
                 letter[i] = letString.charAt(i);
				     if ((Character.isLetter(letter[i]) == true) || (letter[i]==' '))
					  {			 
				         lengthString = Integer.toString(length);
					      vowelsRL.setText(" ");
					      consonantsRL.setText(" ");
				         lengthRL.setText(lengthString);
					  }
					  else
					  {
                     vowelsRL.setText(" ");
					      consonantsRL.setText(" ");
				         lengthRL.setText("INVALID INPUT!");
							errorCheck = true;
							break;
						}
			     }
				  return errorCheck;
			 }
	  
	  		public boolean getVowels()
			{     
             String letString = (stringOfLettersTF.getText()), vowString, conString;
             int i, countVow = 0, countCon = 0, countSpace = 0, length = letString.length();
				 char[] letter = new char[length];
				 boolean errorCheck = false;
		  
             for (i = 0; i < letString.length(); i++)
				 {
                 letter[i] = letString.charAt(i);
					  if ((Character.isLetter(letter[i]) == true) || (letter[i] == ' '))
					  {
                     if (letter[i]=='a' || letter[i]=='e' || letter[i]=='i' || letter[i]=='o' || letter[i]=='u')
                     countVow++;
					      if (letter[i]==' ')
					      countSpace++;
							
					      countCon = (letString.length() - countVow) - countSpace;
                     vowString = Integer.toString(countVow);
				         conString = Integer.toString(countCon);
							lengthRL.setText(" ");
				         vowelsRL.setText(vowString); 
				         consonantsRL.setText(conString);
					  }
					  else
					  {
							errorCheck = true;
							break;
						}
				 }
				 return errorCheck;
         }
			 
	  public static void main(String[] args)
	  {
	      LetterStringManip stringObject = new LetterStringManip();			
	  }	  
}

The masked field allows you to specify what is allowed to be entered in the field. Anything not allowed by the mask does not show up, so terminating after three invalid attempts doesn't really apply there.

This question has already been answered. Start a new discussion instead.