3 errors and then terminate program
Hello all, I'm writing a program that inputs a string of letters and lets the user get the length of the string and/or the number of vowels and consonants in the string (oh, spaces are allowed as input). I have been able to validate my input, but here's the catch, I need the program to terminate when the user chooses to, which it does, and I need the program to terminate after either button is pushed three times with invalid input. You see how I have it set up now, it obviously resets the counter each time the button is pushed. Any suggestions?
Code:
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;
}
}
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;
}
}
return errorCheck;
}
public static void main(String[] args)
{
LetterStringManip stringObject = new LetterStringManip();
}
}