Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd TopKWords.java and fix one typos #406
Conversation
| /* 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 { |
christianbender
Apr 8, 2018
Collaborator
Remove unnecessary spaces between public, class and the class name.
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) { |
christianbender
Apr 8, 2018
Collaborator
Write simple
while (in != -1) {
}
Write simple
while (in != -1) {
}|
|
||
| while (notEnd) { | ||
| // when in == -1 means get the end of the file | ||
| if (-1 == in) { |
christianbender
Apr 8, 2018
Collaborator
See my comment above. You can avoid this if
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 |
christianbender
Apr 8, 2018
Collaborator
Instead of StringBuffer use String
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 |
christianbender
Apr 8, 2018
Collaborator
Use String and the operator +=
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())) { |
christianbender
Apr 8, 2018
Collaborator
See my comments above. You can avoid .toString()
See my comments above. You can avoid .toString()
| dictionary.put(sb.toString(), 1); | ||
| } | ||
| } | ||
| sb = new StringBuffer(); //reload the StringBuffer |
christianbender
Apr 8, 2018
Collaborator
Write instead sb = ""; . See my comments above.
Write instead sb = ""; . See my comments above.
| } | ||
| } | ||
|
|
||
| class CountWords { |
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'
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()); |
christianbender
Apr 8, 2018
Collaborator
Instead of Integer k = new Integer(input.nextLine()); write int k = input.nextInt();
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()); |
christianbender
Apr 8, 2018
Collaborator
Read my comment above.
Read my comment above.
All has been improved according to your advice.
|
All has been improved according to your advice, please check it. |
|
Good Work! |
|
|
||
| fis = new FileInputStream(fileName); // open the file | ||
| int in = 0; | ||
| String s = new String(); // init a empty word |
christianbender
Apr 9, 2018
Collaborator
You don't need new String() you can simply write String s = "";
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()); | |||
christianbender
Apr 9, 2018
Collaborator
Change this line k = new Integer(input.nextLine());, too.
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. |
|
I have already sent to the first email address a couple days ago, should I sent both? |


No description provided.