Error Counter

Placement of error counter

Page 1 of 1

1 Replies - 1848 Views - Last Post: 19 May 2008 - 04:58 PM Rate Topic: -----

#1 hezfast2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-May 08

Error Counter

Post icon  Posted 19 May 2008 - 04:47 PM

Hello all, i've written a GUI program that inputs a string of letters from the user. The input must be letters (spaces are allowed). I'm implementing two event handlers for two buttons, one to get the length of the string, the other to get the number of vowels and consonants. The program runs until the user chooses to terminate. If the user inputs a string that has invalid characters, an error message will display. The user has 3rd time the user inputs a string with invalid characters the program should terminate.

This is a homework assignment, I've gotten it all to work except for my error counter. I know why it's not working, it is part of the event handler and the counter is reset each time the button is pressed so it will not increment. I can't figure out how to implement the counter now though. This is the code that I've written so far, with the error counter where I've been trying it. If anyone can give me some direction here, I'd really appreciate it, I'm losing it here!

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++;
				}
				if (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();			
	  }
	  
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error Counter

#2 hezfast2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-May 08

Re: Error Counter

Posted 19 May 2008 - 04:58 PM

I figured it out, thanks anyways
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1