Advertisement
Crenox

Word Sort Java Program

Mar 6th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. 10
  2. freddy
  3. at
  4. elephant
  5. whoooooodat
  6. alice
  7. tommy
  8. bobby
  9. it
  10. at
  11. about
  12. ------------------------------------------------------------------------------------------------------------------------------
  13. // Name: Sammy Samkough
  14. // Prog: Word Sort
  15. // Spec: Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have
  16. // more than one word with the same length, that group would be sorted alphabetically.
  17.  
  18. public class Word implements Comparable<Word>
  19. {
  20. private String word;
  21.  
  22. public Word(String s)
  23. {
  24. word = s;
  25. }
  26.  
  27. public int compareTo(Word rhs)
  28. {
  29. if(word.length() > rhs.toString().length())
  30. {
  31. return 1;
  32. }
  33. if(word.length() < rhs.toString().length())
  34. {
  35. return -1;
  36. }
  37. if(word.length() == rhs.toString().length())
  38. {
  39. for(int i = 0; i < word.length(); i++)
  40. {
  41. if(word.charAt(i) > rhs.toString().charAt(i))
  42. {
  43. return 1;
  44. }
  45. if(word.charAt(i) < rhs.toString().charAt(i))
  46. {
  47. return -1;
  48. }
  49. else
  50. {
  51.  
  52. }
  53. }
  54. }
  55.  
  56. return 0;
  57.  
  58. }
  59.  
  60. public String toString()
  61. {
  62. return "Word: " + word;
  63. }
  64. }
  65. ------------------------------------------------------------------------------------------------------------------------------
  66. // Name: Sammy Samkough
  67. // Prog: Word Sort
  68. // Spec: Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have
  69. // more than one word with the same length, that group would be sorted alphabetically.
  70.  
  71. import java.io.File;
  72. import java.io.IOException;
  73. import java.util.Scanner;
  74. import java.util.Arrays;
  75.  
  76. public class WordRunner
  77. {
  78. public static void main( String args[] ) throws IOException
  79. {
  80. Scanner file = new Scanner(new File("words.dat"));
  81. int size = file.nextInt();
  82. Word [] words = new Word[size];
  83. file.nextLine();
  84.  
  85. for(int i = 0; i < size; i++)
  86. {
  87. words[i] = new Word(file.next());
  88. }
  89.  
  90. System.out.println("Words before it was sorted:");
  91. for(int i = 0; i < size; i++)
  92. {
  93. System.out.println(words[i]);
  94. }
  95.  
  96. System.out.println("\n======= \n");
  97.  
  98.  
  99. System.out.println("Words after it was sorted:");
  100. Arrays.sort(words);
  101. for(int i = 0 ; i < size ; i++)
  102. {
  103. System.out.println(words[i]);
  104. }
  105.  
  106. System.out.println("\n");
  107. }
  108. }
  109. /*
  110. Words before it was sorted:
  111. Word: freddy
  112. Word: at
  113. Word: elephant
  114. Word: whoooooodat
  115. Word: alice
  116. Word: tommy
  117. Word: bobby
  118. Word: it
  119. Word: at
  120. Word: about
  121.  
  122. =======
  123.  
  124. Words after it was sorted:
  125. Word: about
  126. Word: at
  127. Word: it
  128. Word: bobby
  129. Word: tommy
  130. Word: alice
  131. Word: at
  132. Word: whoooooodat
  133. Word: elephant
  134. Word: freddy
  135.  
  136.  
  137. Press any key to continue . . .
  138. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement