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: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

Program Help, working with spaces and only letters

 
0
  #1
May 18th, 2008
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

  1. import java.io.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6.  
  7. public class LetterStringManip extends JFrame
  8. {//declaring variables
  9. private JLabel stringOfLettersL, lengthL, lengthRL, vowelsL, vowelsRL, consonantsL, consonantsRL;
  10. private JTextField stringOfLettersTF;
  11. private JButton lengthB,vowelB, quitB;
  12. private LengthButtonHandler lbHandler;
  13. private VowelButtonHandler vbHandler;
  14. private QuitButtonHandler qbHandler;
  15. private static final int WIDTH = 750;
  16. private static final int HEIGHT = 150;
  17.  
  18. public LetterStringManip()
  19. {// setting up GUI window with buttons
  20. stringOfLettersL = new JLabel("Enter a String of Letters", SwingConstants.CENTER);
  21. lengthL = new JLabel("Length of String: ", SwingConstants.CENTER);
  22. lengthRL = new JLabel("", SwingConstants.LEFT);
  23. vowelsL = new JLabel("Number of Vowels: ", SwingConstants.CENTER);
  24. vowelsRL = new JLabel("", SwingConstants.LEFT);
  25. consonantsL = new JLabel("Number of Consonats: ", SwingConstants.CENTER);
  26. consonantsRL = new JLabel("", SwingConstants.LEFT);
  27.  
  28. stringOfLettersTF = new JTextField(15);
  29.  
  30. lengthB = new JButton("Length");
  31. lbHandler = new LengthButtonHandler();
  32. lengthB.addActionListener(lbHandler);
  33.  
  34. vowelB = new JButton("Vowels");
  35. vbHandler = new VowelButtonHandler();
  36. vowelB.addActionListener(vbHandler);
  37.  
  38. quitB = new JButton("Quit");
  39. qbHandler = new QuitButtonHandler();
  40. quitB.addActionListener(qbHandler);
  41.  
  42. setTitle("String Of Letters");//Window title
  43.  
  44. Container pane = getContentPane();
  45.  
  46. pane.setLayout(new GridLayout(3,4)); //Window layout
  47.  
  48. pane.add(stringOfLettersL);
  49. pane.add(stringOfLettersTF);
  50. pane.add(lengthL);
  51. pane.add(lengthRL);
  52. pane.add(vowelsL);
  53. pane.add(vowelsRL);
  54. pane.add(consonantsL);
  55. pane.add(consonantsRL);
  56. pane.add(lengthB);
  57. pane.add(vowelB);
  58. pane.add(quitB);
  59.  
  60.  
  61. setSize(WIDTH, HEIGHT);
  62. setVisible(true);
  63. setDefaultCloseOperation(EXIT_ON_CLOSE);
  64.  
  65. }//closes public LetterStringManip()
  66.  
  67. private class LengthButtonHandler implements ActionListener
  68. {
  69. public void actionPerformed(ActionEvent e)//Length Button is clicked, getLength() method is performed
  70. {
  71.  
  72. getLength();
  73.  
  74. }//closes public void actionPerformed(ActionEvent e)
  75.  
  76. }//closes private class LengthButtonHandler implements ActionListener
  77.  
  78. private class VowelButtonHandler implements ActionListener
  79. {
  80. public void actionPerformed(ActionEvent e)//Vowels Button is clicked, getVowels() method is performed
  81. {
  82.  
  83. getVowels();
  84.  
  85. }//closes public void actionPerformed(ActionEvent e)
  86.  
  87. }//closes private class VowelButtonHandler implements ActionListener
  88.  
  89. private class QuitButtonHandler implements ActionListener//Quit Buttton is pressed
  90. {
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. System.exit(0);
  94.  
  95. }//closes public void actionPerformed(ActionEvent e)
  96.  
  97. }//closes private class QuitButtonHandler implements ActionListener
  98.  
  99. public void getLength()//gets the length of the string
  100. {
  101. String letString = (stringOfLettersTF.getText());
  102. String lengthString;
  103. int length;
  104. length = letString.length();
  105. lengthString = Integer.toString(length);
  106. lengthRL.setText(lengthString);
  107. }//closes public void getLength()
  108.  
  109. public void getVowels()//gets the number of vowels in the string
  110. {
  111. String letString = (stringOfLettersTF.getText());
  112. String vowString;
  113. String conString;
  114. int countVow = 0;
  115. int countCon = 0;
  116. int i;
  117. int length = letString.length();
  118. char[] letter = new char[length];
  119.  
  120. for (i = 0; i < letString.length(); i++)
  121. {
  122. letter[i] = letString.charAt(i);
  123. if (letter[i]=='a' || letter[i]=='e' || letter[i]=='i' || letter[i]=='o' || letter[i]=='u')
  124. countVow++;
  125. }//closes for
  126.  
  127. countCon = letString.length() - countVow;
  128. vowString = Integer.toString(countVow);
  129. conString = Integer.toString(countCon);
  130. vowelsRL.setText(vowString);
  131. consonantsRL.setText(conString);
  132.  
  133. }//closes public void getVowels()
  134.  
  135. public static void main(String[] args)
  136. {
  137. LetterStringManip stringObject = new LetterStringManip();
  138.  
  139. }// closes public static void main(String[] args)
  140.  
  141. }//closes public class LetterStringManip extends JFrame
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Program Help, working with spaces and only letters

 
0
  #2
May 20th, 2008
What's the problem ?
See help on code tags, would be more readable if you add =java.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 32
Reputation: hezfast2 is an unknown quantity at this point 
Solved Threads: 0
hezfast2 hezfast2 is offline Offline
Light Poster

Re: Program Help, working with spaces and only letters

 
0
  #3
May 20th, 2008
No problem, figured it out, thanks though
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC