Java method to count the characters of a string

charactercountjavanetbeansstring

I have a string named text and I want to count the number of its characters.
But there is no method 'count' like in ObjC.

In the Internet I found the method 'length', which could be useful. But If I write:

private javax.swing.JTextField txtText;
    int counter = txtText.length();

the compiler complains: "cannot find the symbol"

I thought I need a framework to include, I am absolutely new in Java.

Best Solution

You must get the text from your GUI-component first:

private javax.swing.JTextField jTextField;

String text = jTextField.getText();
int counter = text.length();

JTextField itself is a GUI-Component and not a String. But getText() method gives you the text entered in this component as a String which you can then use to determine the length.