wordsCount challenge

Hi,

I wrote as below and passing all tests
public int wordsCount(String[] words, int len) {
int count=0;
for(int i=0;i<words.length;i++){
  if(words[i].length()==len){
    count++;
    
  }
  
  
}
return count;
  
}

Open in new window


any improvement to this?
LVL 7
gudii9Asked:
Who is Participating?
 
awking00Connect With a Mentor Commented:
Only a slight modification to what you have done -
int count = 0;
for (String word : words) {
  if (word.length == len) {
    count++;
  }
}
return count;
0
 
zzynxSoftware engineerCommented:
>> any improvement to this?
Yes.
1) Give us the url of the challenge
2) Correctly indent your code
0
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
You didn't link to the problem but I can pretty much deduce the requirements from the code. I wouldn't have done anything different to what you have done.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
zzynxSoftware engineerCommented:
@awking00
That's a modification. But is it an improvement?
Also, only advisable when gudii9 is working with Java 8
0
 
awking00Commented:
It wasn't meant as an improvement. I was only trying to show gudii9 an alternate means of looping through an array other than incrementing an index.
0
 
zzynxSoftware engineerCommented:
It wasn't meant as an improvement.
It's good that that is cleared out now. Since he asked for improvements.

Correction: my remark about Java 8 is incorrect.
0
 
gudii9Author Commented:
for (String word : words) {
i like above.
0
 
gudii9Author Commented:
for each loop right?
0
 
gudii9Author Commented:
oops here is the URL of the problem.
http://codingbat.com/prob/p124620
 How to indent codingbat code without starting eclipse which some times takes time?

I used

http://codebeautify.org/javaviewer

it gave exact output after beautifying
beautify.png
0
 
zzynxConnect With a Mentor Software engineerCommented:
for each loop right?
Right

I used
http://codebeautify.org/javaviewer
it gave exact output after beautifying
That gives you an indentation of 1 space. Which is better than nothing.
But that's not what you posted.

How to indent codingbat code without starting eclipse which some times takes time?
By indenting by hand (= adding 4 spaces). It's so worth the effort. You gain readability for yourself and us.

Concerning your real question: there's nothing to improve.
0
All Courses

From novice to tech pro — start learning today.