-
Notifications
You must be signed in to change notification settings - Fork 20.2k
Add TopKWords.java and fix one typos #406
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
Conversation
Others/TopKWords.java
Outdated
/* 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary spaces between public
, class
and the class name.
Others/TopKWords.java
Outdated
in = fis.read(); // read one character | ||
boolean notEnd = true; // signal whether is the end of file | ||
|
||
while (notEnd) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write simple
while (in != -1) {
}
Others/TopKWords.java
Outdated
|
||
while (notEnd) { | ||
// when in == -1 means get the end of the file | ||
if (-1 == in) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment above. You can avoid this if
Others/TopKWords.java
Outdated
|
||
fis = new FileInputStream(fileName); // open the file | ||
int in = 0; | ||
StringBuffer sb = new StringBuffer(); // load the word |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of StringBuffer
use String
Others/TopKWords.java
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use String
and the operator +=
Others/TopKWords.java
Outdated
// 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())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments above. You can avoid .toString()
Others/TopKWords.java
Outdated
dictionary.put(sb.toString(), 1); | ||
} | ||
} | ||
sb = new StringBuffer(); //reload the StringBuffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write instead sb = "";
. See my comments above.
Others/TopKWords.java
Outdated
} | ||
} | ||
|
||
class CountWords { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this class in another file. Or Write it as inline class. Remove the unnecessary spaces between 'class' and 'CountWords'
Others/TopKWords.java
Outdated
); | ||
|
||
Scanner input = new Scanner(System.in); | ||
Integer k = new Integer(input.nextLine()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Integer k = new Integer(input.nextLine());
write int k = input.nextInt();
Others/TopKWords.java
Outdated
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read my comment above.
All has been improved according to your advice.
All has been improved according to your advice, please check it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Work!
Others/TopKWords.java
Outdated
|
||
fis = new FileInputStream(fileName); // open the file | ||
int in = 0; | ||
String s = new String(); // init a empty word |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need new String()
you can simply write String s = "";
Others/TopKWords.java
Outdated
@@ -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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this line k = new Integer(input.nextLine());
, too.
Done. Also, I request to be a member of your team. I have sent an email for a couple of days but no reply. |
@Lisanaaa see In the description you will find two email-addresses. Sent your mail to them. greetings |
I have already sent to the first email address a couple days ago, should I sent both? |
No description provided.