CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Java Swing Word Counter (http://www.codingforums.com/showthread.php?t=220985)

bookittysdad 03-10-2011 02:47 PM

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 


Fou-Lu 03-10-2011 04:54 PM

Quote:

Originally Posted by bookittysdad (Post 1063757)
It just hangs there.

That would typically indicate a non definitive loop.
In this case, variable str is never changed from the original value. Consider using a (it has beena while since I've used a buffered reader, but this looks right): while (null != (str = reader.readLine())) instead.

bookittysdad 03-10-2011 05:04 PM

Thanks, but now I am more confused. This while statement does it have to be in the main method, or another place? I guess what I am asking is where int he code do I put it?

Sorry for being so ignorant, but I am still trying to wrap my head around Java.

Fou-Lu 03-10-2011 05:16 PM

It replaces your current while loop which is infinite in its run.

bookittysdad 03-10-2011 08:19 PM

I tried it, and now I get a Compiler error. reader.readLine symbol not fount. Is this suppose to be dataIn.readLine.

Fou-Lu 03-10-2011 08:22 PM

Yeah it is, my bad.

bookittysdad 03-10-2011 11:49 PM

Thank you for helping. But after i inserted the Code you gave me, and compiled it. I am still getting the CMD.exe window hang. The program just sits there, and does nothing.

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 (null != (str dataIn.readLine()))
                 {
                    
//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 

I am using Win 7 if that helps.

Fou-Lu 03-11-2011 02:47 AM

Loop with this instead, this works fine: while (null != (str = dataIn.readLine()) && !str.isEmpty()). The code itself doesn't really do anything, but you need to interact with the cli interface before it will launch the gui.

bookittysdad 03-11-2011 01:17 PM

Did that, and now I have a Compiling Error. I know you are going out of your way, and I would like to thank you for helping me this far.

Here is the Error.

Quote:

WordCounter.java:37: cannot find symbol
symbol : method isEmpty()
location: class java.lang.String
while (null != (str = dataIn.readLine()) && !str.isEmpty())
Should that be srt.readline.isEmpty? Or do I just need to inport another java class?

Fou-Lu 03-11-2011 01:46 PM

You must be on java 5.
Use str.length > 0 instead of !str.isEmpty.

bookittysdad 03-12-2011 05:12 PM

Thank you, so much.

I replaced it, and the code compiled. But, I am still getting the Hanging at the CMD.exe problem. No error, just hangs there. I also added import java.lang.*.
But still get the hang. I am overlooking something easy I know it. This is due tomorrow night at midnight. I am really starting to stress out here.

Fou-Lu 03-12-2011 06:06 PM

Quote:

Originally Posted by bookittysdad (Post 1064676)
Thank you, so much.

I replaced it, and the code compiled. But, I am still getting the Hanging at the CMD.exe problem. No error, just hangs there. I also added import java.lang.*.
But still get the hang. I am overlooking something easy I know it. This is due tomorrow night at midnight. I am really starting to stress out here.

Have you entered some words hit enter? The frame will not be rendered until you do; the code you have here specifies you must first enter your list of words and then it will construct a new WordCounter object.


All times are GMT +1. The time now is 02:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.