Results 1 to 3 of 3
  1. #1
    hezfast2 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default 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?

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

  2. #2
    hezfast2 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    where would I place a counter to count number of invalid inputs so that the program terminates after 3 tries in a JButton handler so that it doesn't reset each time the button is used?

  3. #3
    hezfast2 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

Similar Threads

  1. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 11:55 PM
  3. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 AM
  4. Trying to catch thread errors
    By yelllow4u in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 02:52 PM
  5. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 05:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •