Reputation: 37
I was making a class that searches for missing files out of a sequence of them (IE: test-1.txt test-2.txt test-4.txt) out of curiosity and when I finally got it to work and I realized I had no idea how to keep checking for missing files after it finds one missing. The problem is that the counter I use to ask for what number in the sequence of files wont work after it finds a missing file because it's permanently off at that point. What I thought might work would be asking what is on the end of the current aFile.getName() and assigning it as an int to i regardless of the fact that it might not be a number(what goes into the actual counter that is a string and is put onto what is checked for). Appearently that doesn't work and gives me:
Exception in thread "main" java.lang.NumberFormatException: For input string: "t"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at TestingClass5.main(TestingClass5.java:43)
Here's what I've got so far:
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import javax.swing.JOptionPane;
public class TestFileSequence {
public static void main(String[] args) {
int i = 0;
//Start Dialogue
String userDefinedFilePath = JOptionPane.showInputDialog("Please input directory"); //File dir
String userDefinedFileName = JOptionPane.showInputDialog("Please input file name"); //File extension
String userDefinedFileType = JOptionPane.showInputDialog("Please input group extension"); //File name
//Start Filter
FilenameFilter userDefinedFilter = new FilenameFilter() {
public boolean accept(File file, String name) {
if (name.endsWith(userDefinedFileType)) {
return true;
} else {
return false;
}
}
};
//Start Array
File dir = new File(userDefinedFilePath);
File[] files = dir.listFiles(userDefinedFilter);
//Check for files matching description
if (files.length == 0) {
System.out.println("The directory doesn't contains any matching files. Please check the directory, extension, and name specified below for accuracy.");
System.out.println("Directory Specified: " + userDefinedFilePath);
System.out.println("Extension Specified: " + userDefinedFileType);
System.out.println("Group Name Specified: " + userDefinedFileName);
} else {
//Check files for gaps in sequence
for (File aFile: files) {
String counter = Integer.toString(i);
String check = userDefinedFileName + "-" + counter + userDefinedFileType;
if (aFile.getName().equals(check)) {
System.out.println("File: " + aFile.getName() + " is present.");
i++;
} else {
System.out.println("Checked for :" + check);
System.out.println("What was found: " + aFile.getName());
String resetCounter = aFile.getName().substring(aFile.getName().length() - 1); //Assigns string resetCounter to the last character in the current aFile.getName() regardless if it isn't a number
int resetLength = Integer.parseInt(resetCounter);
i = resetLength;
}
}
}
}
}
Upvotes: 0
Views: 172
Reputation: 533492
You are trying to get the number at the end of the file name
String resetCounter = aFile.getName().substring(aFile.getName().length() - 1);
//Assigns string resetCounter to the last character in the current aFile.getName() regardless if it isn't a number
for a file called test-1.txt
so the last letter is t
not 1
You could get the digit before the last .
or after the first one or find any number in the file name instead. I suspect you want the number e.g. test-10.txt
should be 10
not 0
You could do
String resetCounter = aFile.getName().replaceAll("[^0-9]+", "");
This just keeps the digits.
Upvotes: 1