Solved

wordsCount challenge

Posted on 2016-08-28
10
59 Views
Last Modified: 2016-08-30
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?
0
Comment
Question by:gudii9
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> any improvement to this?
Yes.
1) Give us the url of the challenge
2) Correctly indent your code
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
Comment Utility
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
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
Only a slight modification to what you have done -
int count = 0;
for (String word : words) {
  if (word.length == len) {
    count++;
  }
}
return count;
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
@awking00
That's a modification. But is it an improvement?
Also, only advisable when gudii9 is working with Java 8
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
for (String word : words) {
i like above.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
for each loop right?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 125 total points
Comment Utility
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now