1

I'm having the hardest time finding out how to code how many words there are in the input for the JTextField, I have a set a clear input button, and once I figure out how to find out how many words there are, I'll be able to clear that as well. Thanks guys here's my code!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CopyTextPanel extends JPanel
{
private JTextField input;
private JLabel output, inlabel, outlabel;
private JButton compute, clear;
private JPanel panel;

public CopyTextPanel()
{
    inlabel = new JLabel("Input Text:  ");
    outlabel = new JLabel("Text Statistics Results: ");
    input = new JTextField (" ", 25);
    output = new JLabel();
    compute = new JButton("Compute Statistics");
    compute.addActionListener (new ButtonListener());
    clear = new JButton("Clear Text");
    clear.addActionListener (new ButtonListener());
    panel = new JPanel();

    output.setPreferredSize (new Dimension(550, 30));
    panel.setPreferredSize (new Dimension(620, 100));
    panel.setBackground(Color.gray);
    panel.add(inlabel);
    panel.add(input);
    //panel.add(outlabel);
    //panel.add(output);
    panel.add(compute);
    panel.add(clear);
    panel.add(outlabel);
    panel.add(output);

    setPreferredSize (new Dimension(700, 150));
    setBackground(Color.cyan);
    add(panel);
}

private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource()==compute)
        {
            {
                output.setText (input.getText());                     
            }
        }
        else
            input.setText("");
    }
}
GonePhisin
  • 45
  • 6
  • 1
    Your basic problem can be better summed up as, "how to count the number of words in a string", I'd suggest do some research on that subject first, as the rest of the code has little to do with solving that issue – MadProgrammer Oct 17 '17 at 21:33
  • You should look into regular expressions. They are very useful for doing what you are trying to do. – luckydog32 Oct 17 '17 at 21:39

1 Answers1

4

For small piece of text like that one in an inputText you could use split to generate a string array with the string break into words and so read the array's length:

String test = "um dois      tres quatro        cinco ";
String [] splitted = test.trim().split("\\p{javaSpaceChar}{1,}");
System.out.println(splitted.length);

//output 5

So, for your input:

String inputText = input.getText();
String [] splitted = inputText.trim().split("\\p{javaSpaceChar}{1,}");
int numberOfWords = splitted.length;
Duloren
  • 2,395
  • 1
  • 25
  • 36
  • Amazing! Thank you! I got it to work and now I'm just wondering if I understand it properly. I understand the inputtext variable sets the intput.getText(); to a string. What I don't understand it why I get an error if I just put Stringp[] splitted = inputText.split("\\s"); for spaces and why I need to add all the other method calls. Thanks againA – GonePhisin Oct 17 '17 at 23:37
  • I did thank you! Could you help me understand better with the String array works instead of just typing in Stringp[] splitted = inputText.split("\\s"); – GonePhisin Oct 17 '17 at 23:46
  • You are welcome. Well, the two approach are similar in some way. The solution tho use is a kind of more robust since cover a all blank spaces situations. Please refer to this answer to more accurate response: https://stackoverflow.com/a/4731164/3055724 – Duloren Oct 18 '17 at 00:17