Advertisement
Guest User

Lab Word Sort

a guest
Oct 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.text.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.lang.*;
  5.  
  6. public class workingWordSort{
  7. public static void main(String[] args) throws Exception{
  8. File file = new File(args[0]);
  9. Scanner input = new Scanner(file);
  10. TreeSet<String>list = new TreeSet<String>();
  11.  
  12. int numW=0;
  13. //Finds number of words for the length of the array and puts it in the list
  14. while(input.hasNextLine()){
  15. if(input.hasNext()){
  16. list.add(input.next());
  17. numW++;
  18. }}
  19. String a[] = list.toArray(new String[0]);
  20.  
  21. System.out.print("Your sorted text file is: ");
  22.  
  23. Finish(a);
  24. for(int i=0;i<a.length;i++) {
  25. System.out.print(a[i] + " ");
  26. }
  27. }
  28.  
  29. public static String[] Finish(String a[]){
  30. // for(int i = 0; i < a.length / 2; i++){ //This is if we needed to print it in descending order
  31. // String temp = a[i];
  32. // a[i] = a[a.length - i - 1];
  33. // a[a.length - i - 1] = temp;
  34. // }
  35. for(int j=0;j<a.length;j++){
  36. if(hasLeadingNumber(a[j])==true){
  37. a[j]="";
  38. }
  39. }
  40. return a;
  41. }
  42. public static boolean hasLeadingNumber(String str){
  43. boolean b = true;
  44. String n = str.substring(0,1);
  45. try{int num = Integer.parseInt(n);}
  46. catch(Exception e){
  47. b = false;
  48. }
  49. return b;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement