943,701 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 772
  • Java RSS
May 18th, 2008
0

Program Help, working with spaces and only letters

Expand Post »
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)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
hezfast2 is offline Offline
32 posts
since Apr 2008
May 20th, 2008
0

Re: Program Help, working with spaces and only letters

What's the problem ?
See help on code tags, would be more readable if you add =java.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
May 20th, 2008
0

Re: Program Help, working with spaces and only letters

No problem, figured it out, thanks though
Reputation Points: 10
Solved Threads: 0
Light Poster
hezfast2 is offline Offline
32 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Colour Cube Faces
Next Thread in Java Forum Timeline: Host-On-Demand (Java Web Start Program)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC