Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 03-10-2011, 04:54 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by bookittysdad View Post
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-10-2011, 05:04 PM   PM User | #3
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
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.
bookittysdad is offline   Reply With Quote
Old 03-10-2011, 05:16 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It replaces your current while loop which is infinite in its run.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-10-2011, 08:19 PM   PM User | #5
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
I tried it, and now I get a Compiler error. reader.readLine symbol not fount. Is this suppose to be dataIn.readLine.
bookittysdad is offline   Reply With Quote
Old 03-10-2011, 08:22 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah it is, my bad.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-10-2011, 11:49 PM   PM User | #7
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
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.
bookittysdad is offline   Reply With Quote
Old 03-11-2011, 02:47 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
bookittysdad (03-11-2011)
Old 03-11-2011, 01:17 PM   PM User | #9
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
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?
bookittysdad is offline   Reply With Quote
Old 03-11-2011, 01:46 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You must be on java 5.
Use str.length > 0 instead of !str.isEmpty.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-12-2011, 05:12 PM   PM User | #11
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
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.
bookittysdad is offline   Reply With Quote
Old 03-12-2011, 06:06 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by bookittysdad View Post
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 AM.


Advertisement
Log in to turn off these ads.