当前位置: 动力学知识库 > 问答 > 编程问答 >

java - using string methods to count words

问题描述:

I am struggling with the below assignment:

assignment q:

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 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 textbox that cannot be modified by the user.

now my problem is that i am not getting the counted number to display in the un-editable textbox.

i also have the problem where the cusrsor is showing in the middle of the input screen instead of at the top.

please can you point me in the right direction.

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;

import java.util.*;

public class WordCounter extends JFrame implements ActionListener

{

//Construct a panel for the fields and buttons

JPanel fieldPanel = new JPanel();

JPanel buttonPanel = new JPanel();

//Construct labels and text boxes

JTextField screen = new JTextField(1);

JLabel wordCount = new JLabel(" Word Count = ");

JTextField words = new JTextField(3);

//Construct button

JButton countButton = new JButton("Count Words");

public static void main(String[] args)

{

WordCounter f = new WordCounter();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(500,200);

f.setTitle("Word Counter");

f.setResizable(false);

f.setLocation(200,200);

f.setVisible(true);

}

public static int getWordCount(String screen)

{

int count = 0;

for (int i = 0; i < screen.length(); i++)

{

if (screen.charAt(i) == ' ')

{

count++;

}

}

return count;

}

public WordCounter()

{

Container c = getContentPane();

c.setLayout((new BorderLayout()));

fieldPanel.setLayout(new GridLayout(1,1));

buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

//add rows to panels

fieldPanel.add(screen);

//add button to panel

buttonPanel.add(countButton);

buttonPanel.add(wordCount);

buttonPanel.add(words);

//add panels to frame

c.add(fieldPanel, BorderLayout.CENTER);

c.add(buttonPanel, BorderLayout.SOUTH);

//add functionality to button

countButton.addActionListener(this);

addWindowListener(

new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?", "File Submission",JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)

System.exit(0);

}

}

);

}

public void actionPerformed(ActionEvent e)

{

}

}

网友答案:

To display the word count, you must modify your actionPerformed method like so:

public void actionPerformed(ActionEvent e)
{
    words.setText(String.valueOf(getWordCount(screen.getText())));
}

And also your method for counting words produces incorrect result if the entered text doesn't end with a space. You could modify your getWordCount method for example like this to get correct word count:

public static int getWordCount(String screen)
{
    String[] words = screen.split("\\s+");
    return words.length;
}

And for your second problem: your cursor displays in the center because JTextField is a single line input. Use JTextArea instead. It is after all specified in your question that you should use it:

JTextArea screen = new JTextArea();
网友答案:

try it:

public static int getWordCount(String screen) { int count = 0;

string[] words = screen.split(' ');

count = words.Length;
return count;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: