Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.*;
- import java.util.*;
- import java.io.*;
- import java.lang.*;
- public class workingWordSort{
- public static void main(String[] args) throws Exception{
- File file = new File(args[0]);
- Scanner input = new Scanner(file);
- TreeSet<String>list = new TreeSet<String>();
- int numW=0;
- //Finds number of words for the length of the array and puts it in the list
- while(input.hasNextLine()){
- if(input.hasNext()){
- list.add(input.next());
- numW++;
- }}
- String a[] = list.toArray(new String[0]);
- System.out.print("Your sorted text file is: ");
- Finish(a);
- for(int i=0;i<a.length;i++) {
- System.out.print(a[i] + " ");
- }
- }
- public static String[] Finish(String a[]){
- // for(int i = 0; i < a.length / 2; i++){ //This is if we needed to print it in descending order
- // String temp = a[i];
- // a[i] = a[a.length - i - 1];
- // a[a.length - i - 1] = temp;
- // }
- for(int j=0;j<a.length;j++){
- if(hasLeadingNumber(a[j])==true){
- a[j]="";
- }
- }
- return a;
- }
- public static boolean hasLeadingNumber(String str){
- boolean b = true;
- String n = str.substring(0,1);
- try{int num = Integer.parseInt(n);}
- catch(Exception e){
- b = false;
- }
- return b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement