Hi all,

Im fairly (very) new to coding. now I have this school project I need to do for IT which is due tonight.
Basicly what I have to do is count the amount of words and letters in a string using what we know, which is not much...
What I was thinking of doing was the following:

enter string
count the length
substring it (using a while or for statement together with the lenght)
test those substrings against being a space or a letter
Count them up and print them.

I'm using visualcafe and the only thing I need to worry about is the actionperformed which is at the bottom of my code.

Here is the code of my mock-up that I have so far:

/*
	A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.applet.*;

public class Applet1 extends Applet
{
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		symantec.itools.lang.Context.setApplet(this);
	
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(null);
		setBackground(java.awt.Color.lightGray);
		setSize(426,266);
		add(InvoerVeld);
		InvoerVeld.setBounds(36,24,100,40);
		DoeKnop.setLabel("Bereken tekens");
		add(DoeKnop);
		DoeKnop.setBackground(java.awt.Color.lightGray);
		DoeKnop.setBounds(12,84,120,40);
		add(UitvoerTekens);
		UitvoerTekens.setBounds(24,168,100,40);
		add(UitvoerWoorden);
		UitvoerWoorden.setBounds(144,168,100,40);
		add(UitvoerLetters);
		UitvoerLetters.setBounds(264,168,100,40);
		//}}
	
		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		DoeKnop.addActionListener(lSymAction);
		//}}
	}
	
	//{{DECLARE_CONTROLS
	java.awt.TextField InvoerVeld = new java.awt.TextField();
	java.awt.Button DoeKnop = new java.awt.Button();
	java.awt.TextField UitvoerTekens = new java.awt.TextField();
	java.awt.TextField UitvoerWoorden = new java.awt.TextField();
	java.awt.TextField UitvoerLetters = new java.awt.TextField();
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == DoeKnop)
				DoeKnop_ActionPerformed(event);
		}
	}

	void DoeKnop_ActionPerformed(java.awt.event.ActionEvent event)
	{
		String entry;
		entry = InvoerVeld.getText();
		int characters;
		characters = entry.length();
		UitvoerTekens.setText(Integer.toString(characters));
		int countLetters;
		countLetters = 0;
		int countWords;
		countWords = 1;

		for (int i = 1; i <= characters; i++)
		{
		    entry.substring(a,b);
		}
		UitvoerWoorden.setText(Integer.toString(tellerWoorden));
		UitvoerLetters.setText(Integer.toString(tellerLetters));
	

	}
}

What you could do is :
1. Ask the string to be entered.
2. Trim it so that any leading/trailing spaces are removed.
3. Split it on the basis of (internal) spaces so that you can get the individual words in it.
4. Count the number of words.
5. Count the length of each word.
6. Sum it up to get the total letters in the string (assuming you do not want to consider the internal space characters)

I've got the first one of those covered, but then I dont have a clue how to do the other 5 steps...
how do I split on internal spaces?
on the basis of the number of internal spaces I can count the number of words,
But what if for example there is a dot in the text I am counting? wouldn't that be in the length of each word?

and do I need to import more lib files? I've only got java.awt and java.applet included...

edit: I'm sorry if this looks like trying to get a quick fix, I am looking for one, because I need to finish this program tonight...

I will however show the effort to learn. I just read the rules (new to this forum) and will do my best to see where I can get.


edit2 :I am finding it had to imply the split I have found. I need to use arrays but I dont have any clue as to how to do this

2. Use the trim() method of the String class to remove leading/trailing white spaces.

3. You can use the split() method to separate words.[Use split(" ") which would specify space as the splitting character]

4.The split method will return you an array containing the splitted elements. Use the length property of the array to get the length of the array which would also be the number of words in the string.

5. Use the length() method on each of the array elements (words) which would be strings in themselves. This will give you the length for each individual word.

6. Sum up the length of each word to get the total number of letters in the original string.

Since the dot or period character (.) would only appear in the end of a word you could write a method of your own using the String classes method to remove it off (see substring()), so that they aren't counted in the letter count.

You would not require to import any packages for the String class since the String class comes under the java.lang package, which is imported by default in every program that you write.

Ok, what I've got now is the following:

void button1_ActionPerformed(java.awt.event.ActionEvent event)
	{
		String invoerRaw;
		invoerRaw = InvoerVeld.getText();
		String invoer;
		invoer = invoerRaw.trim();
		String splitPattern = " ";
	    String[] tokens = invoer.split(splitPattern);
	    int woorden;
	    woorden = tokens.length;
	    textField2.setText(Integer.toString(woorden));
	    
		
	}
}

I've now tried to compile it but my array doesnt seem to be working. would you be able to give an example of how to create the array? I've been trying a bunch of different things I found googling but wasnt able to get any of them to work for me...

the message I'm receiving is:

Error: C:\VisualCafeSE\bin\TempPrj0\Applet1.java(65):  Method split(java.lang.String) not found in class java.lang.String.
        String[] tokens = invoer.split(splitPattern);
                                      ^
textField2.setText(Integer.toString(woorden));

This statement isn't correct. You should rather do

textField2.setText(String.valueOf(woorden));

And as far as the error goes I get none such. Try this code identical to the one written by you. This runs perfectly well at my place. Also the compiler isn't correctly pointing out the error since I had sited you the docs for the split method from the String class itself. (Apart from the fact that I have used this method a thousand odd times)

String str = "disaster posts in the Java Forum";
String splitPattern = " ";
String [] tokens = str.split(splitPattern);
		
for(int i =0;i<tokens.length;i++){
	System.out.println(tokens[i]);
}
commented: Carefully explained +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.