View Single Post
Old 03-10-2011, 02:47 PM   PM User | #1
bookittysdad
New Coder

 
Join Date: Nov 2010
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
bookittysdad is an unknown quantity at this point
Question Java Swing Word Counter

I am a student, and need a little direction. My instructior wants us to use the Swing Method to create a Word Counter in java.

I have it all written out, and it compiles. But the program will not go past the CMD.exe screen. It just hangs there. Any help would be appreciated.

Thanks.

Here is the Code.
PHP Code:
import javax.swing.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;


public class 
WordCounter extends JFrame implements ActionListener
{
    
JPanel panel = new JPanel();
    
JPanel button = new JPanel();
    
JTextArea main = new JTextArea();
    
JTextField result = new JTextField(5);
    
JButton countWords = new JButton("Count the Words");
    
JLabel wordCount = new JLabel("Total Word Count= ");

    public static 
void main(String[] argsthrows IOException
    
{
        
//declaring variables
        
String str;
        
int totalWords 0;
        
int whitespaceCount 0;
        
int length 0;
        
BufferedReader dataIn = new BufferedReader(new InputStreamReader (System.in));
        
str dataIn.readLine();

        
//loop while input is valid
                 
while( str.length() > 0)
                 {
                    
//converting strings
                     
length str.length();

                     
//counting spaces in a string
                      
for (int i 0lengthi++)
                      {
                            if (
Character.isWhitespace(str.charAt(i)))
                            
whitespaceCount++;
                      }

                        
//removing spaces

                           
totalWords =  length-whitespaceCount;
        }


        
//setting layout
        
WordCounter w = new WordCounter();
        
w.setSize(400,400);
        
w.setTitle("Word Count");
        
w.setResizable(false);
        
w.setVisible(true);
        
w.setLocation(300,300);


    }
//end of main method

    
public WordCounter()
    {
        
Container c getContentPane();
        
c.setLayout(new BorderLayout());
        
panel.setLayout(new GridLayout());
        
button.setLayout(new FlowLayout(FlowLayout.CENTER));

        
//adding components to their panels
        
panel.add(main);
        
button.add(countWords);
        
button.add(wordCount);
        
button.add(result);

        
//add panels to frame
        
c.add(panelBorderLayout.CENTER);
        
c.add(button,BorderLayout.SOUTH);

        
//add funtionality to button
        
countWords.addActionListener(this);
    }

    public 
void actionPerformed(ActionEvent e)
    {
    }
}
//end of program 
bookittysdad is offline   Reply With Quote