Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    It replaces your current while loop which is infinite in its run.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried it, and now I get a Compiler error. reader.readLine symbol not fount. Is this suppose to be dataIn.readLine.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah it is, my bad.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    bookittysdad (03-11-2011)

  • #9
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

    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?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You must be on java 5.
    Use str.length > 0 instead of !str.isEmpty.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    New Coder
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •