2 Replies - 464 Views - Last Post: 05 April 2013 - 07:11 AM Rate Topic: -----

#1 spark29  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-November 12

Using string method to count words

Posted 04 April 2013 - 11:28 PM

Hello, I'm kind of new to Java, and cannot figure out how to do one of my assignments.
Using methods from the String class, write a program that will count the number of words, which are separated by blanks in a string. For simplicity, use strings without punctuation or other other white space characters (tabs, newlines, etc). Use a JTextArea to allow the user to enter the text and allow the text area to scroll, if necessary. When the user clicks a button to count the words, the total number of words counted is displayed in a text box that cannot be modified by the user. Allow words to be separated by multiple blanks and by one or more newline characters.
I have wrote the code already and it compiles fine but the output is completely off, the display is not in a JText area and there are no user click buttons. please help.

Thanks

import javax.swing.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;


public class wordCount 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 Words");
	JLabel wordCount = new JLabel("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
		wordCount w = new wordCount();
		w.setSize(450,300);
		w.setTitle("Word Count");
		w.setResizable(false);
		w.setVisible(true);
		w.setLocation(200,200);


	}//end of main method

	public wordCount()
	{
		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



Is This A Good Question/Topic? 0
  • +

Replies To: Using string method to count words

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Using string method to count words

Posted 05 April 2013 - 02:24 AM

Your assignment tell you to use JTextArea for the user to type in the word so do not use InputStream. I dont really understand why your teacher would make it harder for their students by including GUI. What is wrong with a simple, well designed console app.

Anyway.. You can attach the KeyListener to the JTextArea and when the user clicks Enter then you get the whole input and you split it.

Or you can simply use a JButton, and attach a listener to that and perform the same action as above.

ie.

String[] values = textArea.getText().split();
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Using string method to count words

Posted 05 April 2013 - 07:11 AM

this will be true for ever

while( str.length() > 0)


you really don't need a while here. Simply use your for() and if length() == 0 it will simply not be executed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1