Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008, 10:13 PM
Member
 
Join Date: May 2008
Posts: 4
hezfast2 is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-20-2008, 02:52 AM
Member
 
Join Date: May 2008
Posts: 4
hezfast2 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-20-2008, 03:57 AM
Member
 
Join Date: May 2008
Posts: 4
hezfast2 is on a distinguished road
I figured it out, thanks anyway
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with these errors oceansdepth New To Java 3 04-16-2008 06:55 PM
Errors I don't understand MattyB New To Java 4 04-02-2008 01:55 AM
I have 3 errors after compiling coco Database 2 10-18-2007 11:32 AM
Trying to catch thread errors yelllow4u New To Java 2 08-07-2007 04:52 PM
Errors in constructor ai_2007 Advanced Java 0 07-01-2007 07:35 PM


All times are GMT +3. The time now is 05:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org