It is common for people to name directories as dir1, dir2, and so on. When there
ID: 3850934 • Letter: I
Question
It is common for people to name directories as dir1, dir2, and so on. When there are ten or more direct ories, the operating system displays them in dictionary order, as dir1, dir10, dir11, dir12, dir2, dir3, and so on. That is irritating, and it is easy to fix. Provide a comparator that compares strings that end in digit sequences in a way that makes sense to a human. First compare the part before the digit as strings, and then compare the numeric values of the digits. Your program should read in a text file with the list of directories to sort and should output them to the screen. If an error is found while reading the text file, the user should be allowed to choose another file. There will be one directory name per line of the file. Java, netBeans
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.june;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Sam
*/
public class SortDir {
String[] dirs;
public static void main(String[] args) throws IOException {
SortDir obj = new SortDir();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String location = "";
while (!obj.loadDir(location)){
System.out.println("CAnnot read file. Enter new location: ");
location = br.readLine();
}
obj.sort();
obj.print();
}
private boolean loadDir(String location){
try {
BufferedReader br = new BufferedReader(new FileReader(location));
int n = 0;
while (br.readLine()!=null)
n++;
dirs = new String[n];
br = new BufferedReader(new FileReader(location));
String line;
n = 0;
while ((line = br.readLine())!=null)
dirs[n++]=line;
return true;
} catch (FileNotFoundException ex) {
Logger.getLogger(SortDir.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SortDir.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
private void sort() {
String tmp;
for (int i = 0; i < dirs.length; i++)
for (int j = 0; j < dirs.length - i - 1; j++) {
int a = 0, b = 0, pos = 0;
//NOTE: 25 = 5 + 20 = 5*10^0 + 2*10^1
for (int k = dirs[j].length()-1; k >= 0; k--) //extract the number from the right end of the string
if (dirs[j].charAt(k) >= '0' && dirs[j].charAt(k) <= '9')
a += (dirs[j].charAt(k) - '0') * Math.pow(10, pos++);
else
break;
pos = 0; //reset pos which take care of the power
for (int k = dirs[j+1].length()-1; k >= 0; k--) %repeat for j+1th string
if (dirs[j+1].charAt(k) >= '0' && dirs[j+1].charAt(k) <= '9')
b += (dirs[j+1].charAt(k) - '0') * Math.pow(10, pos++);
else
break;
if (a > b) {
tmp = dirs[j];
dirs[j] = dirs[j+1];
dirs[j+1] = tmp;
}
}
}
private void print() {
for (int i = 0; i < dirs.length; i++)
System.out.println(dirs[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.