Visitar URL original
Add TopKWords.java and fix one typos by Lisanaaa · Pull Request #406 · TheAlgorithms/Java · GitHub
The Wayback Machine - https://web.archive.org/web/20201103190521/https://github.com/TheAlgorithms/Java/pull/406
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TopKWords.java and fix one typos #406

Merged
merged 4 commits into from Apr 11, 2018

Conversation

@Lisanaaa
Copy link
Contributor

@Lisanaaa Lisanaaa commented Apr 4, 2018

No description provided.

Lisanaaa added 2 commits Apr 4, 2018
/* display the most frequent K words in the file and the times it appear
in the file – shown in order (ignore case and periods) */

public class TopKWords {

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Remove unnecessary spaces between public, class and the class name.

in = fis.read(); // read one character
boolean notEnd = true; // signal whether is the end of file

while (notEnd) {

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Write simple

while (in != -1) {

}

while (notEnd) {
// when in == -1 means get the end of the file
if (-1 == in) {

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

See my comment above. You can avoid this if


fis = new FileInputStream(fileName); // open the file
int in = 0;
StringBuffer sb = new StringBuffer(); // load the word

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Instead of StringBuffer use String

notEnd = false; //if false, end the while loop
}
if (Character.isLetter((char)in)) {
sb.append((char)in); //if get a letter, put it in StringBuffer

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Use String and the operator +=

// this branch means an entire word has just been read
if (sb.length() > 0) {
//see whether word exists in StringBuffer or not
if (dictionary.containsKey(sb.toString())) {

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

See my comments above. You can avoid .toString()

dictionary.put(sb.toString(), 1);
}
}
sb = new StringBuffer(); //reload the StringBuffer

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Write instead sb = ""; . See my comments above.

}
}

class CountWords {

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Put this class in another file. Or Write it as inline class. Remove the unnecessary spaces between 'class' and 'CountWords'

);

Scanner input = new Scanner(System.in);
Integer k = new Integer(input.nextLine());

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Instead of Integer k = new Integer(input.nextLine()); write int k = input.nextInt();

while (k > list.size()) {
System.out.println("Retype a number, your number is too large");
input = new Scanner(System.in);
k = new Integer(input.nextLine());

This comment has been minimized.

@christianbender

christianbender Apr 8, 2018
Collaborator

Read my comment above.

All has been improved according to your advice.
@Lisanaaa
Copy link
Contributor Author

@Lisanaaa Lisanaaa commented Apr 8, 2018

All has been improved according to your advice, please check it.

Copy link
Collaborator

@christianbender christianbender left a comment

Good Work!


fis = new FileInputStream(fileName); // open the file
int in = 0;
String s = new String(); // init a empty word

This comment has been minimized.

@christianbender

christianbender Apr 9, 2018
Collaborator

You don't need new String() you can simply write String s = "";

@@ -19,74 +70,15 @@ public static void main(String[] a) {
);

Scanner input = new Scanner(System.in);
Integer k = new Integer(input.nextLine());
int k = input.nextInt();
while (k > list.size()) {
System.out.println("Retype a number, your number is too large");
input = new Scanner(System.in);
k = new Integer(input.nextLine());

This comment has been minimized.

@christianbender

christianbender Apr 9, 2018
Collaborator

Change this line k = new Integer(input.nextLine());, too.

@Lisanaaa
Copy link
Contributor Author

@Lisanaaa Lisanaaa commented Apr 9, 2018

Done. Also, I request to be a member of your team. I have sent an email for a couple of days but no reply.

@christianbender
Copy link
Collaborator

@christianbender christianbender commented Apr 11, 2018

@Lisanaaa see In the description you will find two email-addresses. Sent your mail to them. greetings

@christianbender christianbender merged commit 43d3a56 into TheAlgorithms:master Apr 11, 2018
@Lisanaaa
Copy link
Contributor Author

@Lisanaaa Lisanaaa commented Apr 11, 2018

I have already sent to the first email address a couple days ago, should I sent both?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.