Created
November 9, 2012 02:14
-
-
Save parsons-Jsr/4043290 to your computer and use it in GitHub Desktop.
Java Sentence Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Jamie Parsons' Sentence Counter Program*/ | |
import java.awt.*; // Imports advance windows toolkit | |
import java.awt.event.*; // Imports package needed for ActionListener | |
import javax.swing.*; // Imports SWING package | |
import java.util.*; // Imports package for Arraylist | |
public class SentenceProject implements ActionListener // Class definition | |
{ | |
JLabel infoLbl = new JLabel("Please enter your sentence below:"); // Creates a Label that gives instructions for the User | |
JButton mainBtn = new JButton("scan"); // Creates a button with the text "scan". Clicking this creates an ActionEvent | |
JTextField inputTxt = new JTextField(35); // Creates a TextField for user input of sentance | |
final public ImageIcon icon | |
= (new ImageIcon(getClass().getResource("images/SentenceBanner.png"))); // Creates an icon to act as a banner for the program | |
// getResource is to allow access to this icon when the directory is within a jar archive | |
JLabel iconHolder = new JLabel(icon); // Adds the banner image to a label | |
JLabel charTitle = new JLabel("Total Length:"); // Creates label title for Total Length | |
JTextField charOutput = new JTextField(2); // Creates textfield for outputing Total Length | |
JLabel vowelTitle = new JLabel(" Num Vowels:"); // Creates label title for Number of Vowels | |
JTextField vowelOutput = new JTextField(2); // Creates textfield for outputing Total Length | |
JLabel wordTitle = new JLabel(" Longest Word Length:"); // Creates label title for Longest Word length | |
JTextField wordOutput = new JTextField(2); // Creates textfield for outputing the length of the longest word | |
JLabel wordNameTitle = new JLabel("Longest word Name(s): "); // Creates label title for outputing longest word(s) | |
JTextField wordNameOutput = new JTextField(30); // Creates textfield for outputing longest word(s) | |
public SentenceProject() // Default Contructor | |
{ | |
JFrame frame = new JFrame("JSRP SentenceProject ver 0.7"); // GUI Window Frame | |
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); // Tells program to close when frame is closed | |
frame.setVisible(true); // Set frame to be visible | |
frame.setSize(500,200); // Sets the resolution in pixels of the frame | |
frame.setResizable(false); // Stops User from changing the Frame size | |
frame.setLocationRelativeTo(null); // Sets frame location to Center (default = center, thus null) | |
frame.setLayout(new FlowLayout()); // Creates Flow Layout & instance of.. wahy in which objects are positioned on screen | |
frame.getContentPane().add(iconHolder); // Adds the Icon Banner label to the Frame | |
frame.getContentPane().add(infoLbl); // Adds label of instructions to the Frame | |
frame.getContentPane().add(inputTxt); // Adds textfield for user input of sentence to the Frame | |
frame.getContentPane().add(mainBtn); // Adds button that starts the ActionEvent to the Frame | |
frame.getContentPane().add(charTitle); // Adds title for Character Length to the Frame | |
frame.getContentPane().add(charOutput); // Adds textfield that displays character length | |
frame.getContentPane().add(vowelTitle); // Adds title for the vowel output to the Frame | |
frame.getContentPane().add(vowelOutput); // Adds textfield that displays number of vowels to the Frame | |
frame.getContentPane().add(wordTitle); // Adds title for the word length output to the Frame | |
frame.getContentPane().add(wordOutput); // Adds textfield that displays the length of the longest word to the Frame | |
frame.getContentPane().add(wordNameTitle); // Adds title for the longest word(s) to the Frame | |
frame.getContentPane().add(wordNameOutput); // Adds textfield that displays the longest word to the Frame | |
charOutput.setEditable(false); | |
vowelOutput.setEditable(false); // Stops Output textfields from being edited by user | |
wordOutput.setEditable(false); | |
wordNameOutput.setEditable(false); | |
mainBtn.addActionListener(this); // Creates Instance of ActionListener for the main button | |
} | |
public void actionPerformed (ActionEvent e) // Method for ActionListener's effect on the main button | |
{ | |
if(e.getSource() == mainBtn) // If main button is clicked | |
{ // countCharacters is the function that returns overall length | |
charOutput.setText(Integer.toString((countCharacters(inputTxt.getText())))); // Stores length of the longest word, Coverts int to String, Sends to textfield | |
System.out.println("- - -\nchar count: " + countCharacters(inputTxt.getText())); // Output overall number of characters to command window (DEBUG) | |
vowelOutput.setText(Integer.toString((NumberOfVowels(inputTxt.getText())))); // Stores overall number of vowels, Coverts int to String, Sends to textfield | |
System.out.println("vowel count: " + NumberOfVowels(inputTxt.getText())); // Output overall number of vowels to command window (DEBUG) | |
wordOutput.setText(Integer.toString((LongestWordLength(inputTxt.getText())))); // Stores overall length of word, Coverts int to String, Sends to textfield | |
System.out.println("longest word length: " + LongestWordLength(inputTxt.getText())); // Output length of the longest word to command window (DEBUG) | |
wordNameOutput.setText((LongestWordName(inputTxt.getText()))); // Stores variable holding names of longest words, Sends to textfield | |
System.out.println("longest word Name: " + LongestWordName(inputTxt.getText())); // Output length of the longest word(s) to command window (DEBUG) | |
} | |
} | |
public static Integer countCharacters(String input) // Method that counts characters and returns length as integer | |
{ | |
return input.length(); // Returns length of a String | |
} | |
/** This loop first creates a permenant counter (x), aswell as an array that contains all vowels in both upper and lowercase. | |
It then creates a temp counter (i): which states the loop should continue for every character in the user input | |
Within this is a nested loop with a temp counter (a): which states states that this loop should continue for every object in the array. | |
Finally it states that if the currently iterated object in the array is the same as the currently iterated character from the user input, | |
the add 1 the the permenant counter. **/ | |
public int NumberOfVowels(String input) // Method for returning number of vowels | |
{ | |
int x=0; // Creates counter 'x' with an intial value of 0, to store the total amount of vowels | |
char [] vowelArray = {'A','E','I','O','U','a','e','i','o','u'}; // Creates array containing both upper & lowercase vowels | |
for(int i=0; i < input.length(); i++) // for i, loop continues while i < total length of the user input | |
{ | |
for(int j = 0; j < vowelArray.length; j++) // for a, loop continues while a < length of the array | |
{ | |
if(vowelArray[j] == input.charAt(i)) // if object in array at position 'a' is the same as (==) character from the user input at position 'i' | |
{ | |
x+=1; // add 1 to the overall number of vowels (stored in 'x') | |
} | |
} | |
} | |
return x; | |
} | |
public int LongestWordLength(String input) // method for returning length of the longest word | |
{ | |
int x=0; // creates a counter called 'x' | |
String[] words = input.split(" "); // creats an array called 'words' that stores and splits up the user input by interpreting " " as the divide between words | |
for(int i=0; i <words.length; i++) // for i, continue the loop while there are objects left in the array | |
{ | |
if(words[i].length() >= words[x].length()) // if the length of an object in the array > length of the current largest length (stored in 'x') | |
{ | |
x = i; // Then update 'x' to this new value | |
} | |
} | |
return words[x].length(); | |
} | |
public String LongestWordName(String input) // Method for returning the longest words | |
{ | |
int x=0; // Creates counter called x | |
String[] words = input.split(" "); // Creates array to hold list of words that are inputted | |
ArrayList<String> tempList = new ArrayList<String>(); // Creates ArrayList to hold a temporary list of the longest words relative to the previous word | |
ArrayList<String> storeList = new ArrayList<String>(); // Creates ArrayList to hold the final 'true' longest words | |
String longestPrint = ""; // Creates String to Display the Longest words (used for the String return) | |
for(int i=0; i < words.length; i++) // for i, continue the loop while there are objects left in the array | |
{ | |
if(words[i].length() >= words[x].length()) // if the length of an object in the array > length of the current largest length (stored in 'x') | |
{ | |
x = i; // Then update 'x' to this value... | |
tempList.add(words[x]); // ...and add the word at this position to the the temp list | |
} | |
} | |
for (int i = 0; i < tempList.size(); i++) // for i, continue loop while there are objects left in the temp list | |
{ | |
String temp = tempList.get(i); // Create temp variable to get item from list as a string so its length can be determined | |
if(temp.length() == words[x].length()) // if the length of this item from the list >= the length of the longest word | |
{ | |
storeList.add(temp); // Add it to the store list | |
} | |
} | |
for(int i=0; i < storeList.size(); i++) // for i, continue loop while there are objects left in list | |
{ | |
longestPrint += storeList.get(i) + ", "; // Add object as String and separate with ', ' | |
} | |
return longestPrint; // retrun the string now containing longest words | |
} | |
public static void main (String[] args) // Main Method | |
{ | |
new SentenceProject(); // Creates new instance of default constructor | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment