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[] args) throws 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 = 0; i < length; i++)
{
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(panel, BorderLayout.CENTER);
c.add(button,BorderLayout.SOUTH);
//add funtionality to button
countWords.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}//end of program
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
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.
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
__________________
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
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.
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[] args) throws 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 = 0; i < length; i++)
{
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(panel, BorderLayout.CENTER);
c.add(button,BorderLayout.SOUTH);
//add funtionality to button
countWords.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}//end of program
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
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?
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
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.
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