| | |
Program Help, working with spaces and only letters
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Solved Threads: 0
I'm writing a program that takes an input string and gets the length and/or gets the number of vowels and consonants in the string. I've gotten it working except for consideration of spaces (which are allowed) and error checking for input other than letters. Here is what I've got so far. ANY suggestions would be greatly appreciated. Thank You
Java Syntax (Toggle Plain Text)
import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class LetterStringManip extends JFrame {//declaring variables 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() {// setting up GUI window with buttons 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");//Window title Container pane = getContentPane(); pane.setLayout(new GridLayout(3,4)); //Window layout 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); }//closes public LetterStringManip() private class LengthButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e)//Length Button is clicked, getLength() method is performed { getLength(); }//closes public void actionPerformed(ActionEvent e) }//closes private class LengthButtonHandler implements ActionListener private class VowelButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e)//Vowels Button is clicked, getVowels() method is performed { getVowels(); }//closes public void actionPerformed(ActionEvent e) }//closes private class VowelButtonHandler implements ActionListener private class QuitButtonHandler implements ActionListener//Quit Buttton is pressed { public void actionPerformed(ActionEvent e) { System.exit(0); }//closes public void actionPerformed(ActionEvent e) }//closes private class QuitButtonHandler implements ActionListener public void getLength()//gets the length of the string { String letString = (stringOfLettersTF.getText()); String lengthString; int length; length = letString.length(); lengthString = Integer.toString(length); lengthRL.setText(lengthString); }//closes public void getLength() public void getVowels()//gets the number of vowels in the string { 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++; }//closes for countCon = letString.length() - countVow; vowString = Integer.toString(countVow); conString = Integer.toString(countCon); vowelsRL.setText(vowString); consonantsRL.setText(conString); }//closes public void getVowels() public static void main(String[] args) { LetterStringManip stringObject = new LetterStringManip(); }// closes public static void main(String[] args) }//closes public class LetterStringManip extends JFrame
![]() |
Similar Threads
- Textbox allows letters and numbers only (Visual Basic 4 / 5 / 6)
- Null pointer on building a binary tree (Java)
- Java Client/Server (Java)
- Random Files (Introduction) (Visual Basic 4 / 5 / 6)
- Program Help (C++)
- C++ Syntax (C++)
Other Threads in the Java Forum
- Previous Thread: Colour Cube Faces
- Next Thread: Host-On-Demand (Java Web Start Program)
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows





