|
-
April 2nd, 2012, 08:14 PM
#1
counting words in a string array
Hi, I'm new to this site, so please be gentle if I don't do something right.
I'm having a bit of an issue with this code:
Code:
public void actionPerformed(ActionEvent e)
{
String textContents;
textContents = textEntryField.toString();
String[] words = textContents.split("\\s");
int wordCount = 0;
//count number of words
for(int i = 0; i < words.length; i++)
{
if(!words[i].equals("\\s") && !words[i].equals(""))
wordCount++;
}
//set jtextfield to value
wordCountField.setText(Integer.toString(wordCount));
}
What I'm trying to do is split a String, by white-space, and fill an array with it. The string is in a JTextArea.
When a button is clicked, it is supposed to pull the String data from the JTextArea (textEntryField), pass it into a String, split that String by white-space, and throw it into the array. For each entry/word in the array, the counter (wordCount) is supposed to increase by 1. However, no matter what I enter into the JTextArea, I always get a result of 1 and only 1.
Is there anything you guys see that's potentially causing me issues? Thank you in advance for your help.
Side note: There's only one button as of now, hence why there's no testing to see what button caused the actionPerformed.
-
April 3rd, 2012, 12:11 PM
#2
Re: counting words in a string array
textEntryField.toString() gets a string representing the object and not the text entered into it.
To get the text from a JTextArea use textEntryField.getText().
When faced with this sort of problem it's always a good idea to add a few print statements so you can see what is in the variables.
-
April 3rd, 2012, 12:32 PM
#3
Re: counting words in a string array
keang is right. Printing output while testing is a great way to track down errors. In Java it's about as easy as it can get:
System.out.println( [put stuff in here] );
If you really want it to be attention grabbing (at least in Netbeans) do this instead:
System.err.println( [put stuff in here] )
Using "err" instead of "out" will cause the text to appear in red in Netbeans.
The great thing about Java is that it's super easy to make stuff readable. Back in C++ it was cumbersome because most of the time you needed something other than string data printed. So you either had to convert everything to a string or use printf to piece everything together.
In Java you can literally do this:
String name = "Bob";
int age = 25;
MyEnumDays dayOfWeek = WEDNESDAY
Point pt = new Point(50, 60);
System.out.println( name + " is " + age + " years old. Was born on " + dayofWeek + " and loves the point " + pt);
And it just works.
Once you solve the problem you get rid of the print statements.
The most difficult thing about using System.out.println() is that half the time after you're done writing it Netbeans will auto-correct it to something else!
Why are the "tolerant" so easy to offend?
-
April 3rd, 2012, 04:55 PM
#4
Re: counting words in a string array
 Originally Posted by keang
textEntryField.toString() gets a string representing the object and not the text entered into it.
To get the text from a JTextArea use textEntryField.getText().
When faced with this sort of problem it's always a good idea to add a few print statements so you can see what is in the variables.
 Originally Posted by SteveS
keang is right. Printing output while testing is a great way to track down errors. In Java it's about as easy as it can get:
System.out.println( [put stuff in here] );
If you really want it to be attention grabbing (at least in Netbeans) do this instead:
System.err.println( [put stuff in here] )
Using "err" instead of "out" will cause the text to appear in red in Netbeans.
The great thing about Java is that it's super easy to make stuff readable. Back in C++ it was cumbersome because most of the time you needed something other than string data printed. So you either had to convert everything to a string or use printf to piece everything together.
In Java you can literally do this:
String name = "Bob";
int age = 25;
MyEnumDays dayOfWeek = WEDNESDAY
Point pt = new Point(50, 60);
System.out.println( name + " is " + age + " years old. Was born on " + dayofWeek + " and loves the point " + pt);
And it just works.
Once you solve the problem you get rid of the print statements.
The most difficult thing about using System.out.println() is that half the time after you're done writing it Netbeans will auto-correct it to something else!
Thank you both so much. It works perfectly now. A little back story: I originally learned VB.NET, and have only recently started with Java. With VB.NET, it was so easy to just add breakpoints and see while the code runs what data is being passed and received along the way.
While coding this Java counter, I was trying to think of a way to in effect do the same thing, but was drawing a blank. It was actually quite frustrating, since my code changes were basically shots in the dark, as a result of not being able to see what was being passed around. Your guys' approach is a great solution to that problem. I'll certainly make use of that method in the future. Heck, I may even throw a couple of lines in this code just to explore its usefulness. Thank you again.
-
April 3rd, 2012, 05:30 PM
#5
Re: counting words in a string array
You could always use a free IDE such as Eclipse or NetBeans, both of which come with debuggers which allow you to single step through the code, see what values variables have etc, etc.
-
April 3rd, 2012, 06:04 PM
#6
Re: counting words in a string array
 Originally Posted by keang
You could always use a free IDE such as Eclipse or NetBeans, both of which come with debuggers which allow you to single step through the code, see what values variables have etc, etc.
Eclipse I have, but haven't messed around with too much. I've never used NetBeans at all. I didn't know either of those had that function. I've been using TextPad for Java coding. As far as I know, it's not capable of that. Maybe I'll check them out. Thanks.
-
April 4th, 2012, 01:08 AM
#7
Re: counting words in a string array
 Originally Posted by bdbc
Eclipse I have, but haven't messed around with too much. I've never used NetBeans at all. I didn't know either of those had that function. I've been using TextPad for Java coding. As far as I know, it's not capable of that. Maybe I'll check them out. Thanks.
OMG. That's hardly better than chiseling the code on stone tablets!
I'm probably on the verge of blowing my top over something that seems more and more like a Netbeans bug, but since that's not focused in my head right now I can give you my honest overall opinion on Netbeans instead of some knee-jerk short-term emotional outburst:
Netbeans is surprisingly awesome for a free tool, and stands next to the pricey Visual Studio in terms of quality. It color-codes Java keywords and class members. It notifies you of the use of an undefined class or variable. Don't like a file name? Change it in the UI and Netbeans will change all the references from the old name to the new. Hate researching the location of some class so that you can write an import statement? Just right click anywhere in the file and select "Fix Imports". It will add them and will also remove unused imports cluttering up the code.
You can select chunks of code and hit a "//" button to comment out the lines. You can change the package a file belongs to and it will automatically adjust the other code using that file's class. It applies suggestions and will even load the fields of a function automatically with variables present in that function. It's freaky how often it makes the right guesses.
Of course it also has drawbacks:
The autosuggest is too aggressive. Like I said, type "System.out.println(" and you'll often find something else typed. I like Visual Studio's style of debugger better because it's easier to view a list of variables and the stack, but this is a very advanced debugger. It's actually too advanced in some ways because it will allow you to launch the debugger again while you're already in a debug session. This is an issue because it's easy to accidently hit that button.
I tried Eclipse, but only briefly. I was attempting to use design view in that tool when I needed some sort of basic window (I think it was a component's basic properties window) and it was missing and could not be found anywhere. I think I just didn't want to give it a chance because the name reminds me of those awful movies.
Why are the "tolerant" so easy to offend?
-
April 4th, 2012, 01:55 AM
#8
Re: counting words in a string array
I think it can be useful to start writing Java using a basic editor and command line but once you've got to grips with specifying the classpath, packages, debugging with print statements, using java & javac etc then definitely move to an IDE.
There are pro and cons to both Eclipse and Netbeans but both are well featured and amazingly good value
If you want to compare their features and/or people's views on them try google.
-
April 4th, 2012, 11:22 AM
#9
Re: counting words in a string array
If you want to use a free text editor for Java coding take a look at Notepad++. It's a powerful text editor with its own set of plugins etc and I use it for everyday text use - great for copying and pasting and holding code in the background and searching for text across multiple open documents, also it has a great compare documents feature available via a plugin. It also supports Java etc but really you should move to an IDE to move forward in Java as you will need to debug applications which text editors just can't handle.
I'm a learner with Java (my posts will confirm this lol) but I have used both Eclipse and Netbeans - Eclipse wins it for me; I find it more user friendly and it is well supported and documented
-
April 4th, 2012, 12:56 PM
#10
Re: counting words in a string array
I've actually used Notepad++ quite a few times, mainly with modifying Android code. It has a nice find-and-replace function, among other things.
Yesterday, I gave Eclipse another whirl. Given, it's still very early, but I have mixed feelings about it. It's almost as if they tried to make it too capable. But that may just be due to my lack of familiarity with it.
I think in the next few days I'll try out NetBeans, and see what my initial reaction to that is. I agree that with larger and larger apps, it becomes more difficult to get by with an editor such as TextPad. However, it probably did help with the learning experience, as one of you mentioned.
I do have a couple of questions, if someone doesn't mind answering them. For one, do either of these IDEs stand out as significantly more adept at handling Android code? And as a somewhat unrelated question, what are the popular IDEs/editors to use with C#? I am aware that Eclipse, NetBeans, and Visual Studio all have that capability (or so I think), but what seems to work best?
Thank you for all of the input thus far. It is certainly helpful.
-
April 7th, 2012, 02:14 PM
#11
Re: counting words in a string array
lol I can see why this debate always goes back and forth between NetBeans and Eclipse. My initial reaction was to like NetBeans' functionality. However, after giving both a few days, I'm now leaning toward Eclipse. It just seems more refined.
-
April 8th, 2012, 02:55 AM
#12
Re: counting words in a string array
I went through as similar process a number of years ago and ended up using Eclipse. Having said that, the GUI builder and profiler in Netbeans are very good and easy to use.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|