Simple comparison problem
in
Programming Questions
•
2 years ago
I'm reading in log files to determine whether there are missing files in the set. They are consistently named, with servername at the front and hour at the end of the filenames. I need to check whether the current file is from the same server as the previous, and set "same" or "different" booleans to control the following actions. However, I it is resisting such determination and never recognizing the servers as "same". DRIVING ME NUTS. I'm sure there's a simple mistake in the code somewhere. If anyone can give it a few minutes, I'd appreciate your insights.
In comparefiles() I make the comparison, and act accordingly. The following data produces the following output:
Files being processed:
servername1_log-2010-0401-01
servername1_log-2010-0401-02
servername1_log-2010-0401-03
servername2_log-2010-0401-00
servername2_log-2010-0401-01
Output:
Different!
1 |servername1_log|0401| missed | first hour(s) 0 through 0
Different!
1 |servername1_log|0401| missed | last hour(s) 2 through 23
Different!
1 |servername1_log|0401| missed | last hour(s) 3 through 23
Different!
1 |servername1_log|0401| missed | last hour(s) 4 through 23
Different!
1 |servername2_log|0401| missed | last hour(s) 1 through 23
2 |servername2_log|0401| missed | last hour(s) 2 through 23
0401 had |5| files
My code:
In comparefiles() I make the comparison, and act accordingly. The following data produces the following output:
Files being processed:
servername1_log-2010-0401-01
servername1_log-2010-0401-02
servername1_log-2010-0401-03
servername2_log-2010-0401-00
servername2_log-2010-0401-01
Output:
Different!
1 |servername1_log|0401| missed | first hour(s) 0 through 0
Different!
1 |servername1_log|0401| missed | last hour(s) 2 through 23
Different!
1 |servername1_log|0401| missed | last hour(s) 3 through 23
Different!
1 |servername1_log|0401| missed | last hour(s) 4 through 23
Different!
1 |servername2_log|0401| missed | last hour(s) 1 through 23
2 |servername2_log|0401| missed | last hour(s) 2 through 23
0401 had |5| files
My code:
- // globals
String filename = "";
long fileCount = 0;
String missed = "";
String theServer;
String theDay;
String theHour;
String oldServer = "none";
int newHour;
int oldHour = -1;
boolean sameServer;
String station = "";
PrintWriter output;
void setup() {
String dateTimeStamp = "_" + year() + nf(month(),2) + nf(day(),2) + "_" + nf(hour(),2) + "_" + nf(minute(),2);
output = createWriter("FileCheck" + dateTimeStamp + ".txt");
File dataFolder = new File(sketchPath, "data");
String[] fileList = dataFolder.list( );
if (fileList != null) {
for (int i = 0; i < fileList.length; i++) {
filename = fileList[i];
fileCount++;
oldServer = theServer;
parsefilename(filename);
comparefiles(theServer,oldServer,theDay,theHour,oldHour);
oldHour = int(theHour);
}
if (oldHour != 23) { // old server missed last hour(s)
missed = " last hour(s) " + (oldHour+1) + " through 23";
}
station = "2 |";
if (missed != "") {
filestats(station,theServer,theDay,missed);
}
stats(theDay,fileCount);
} else {
println("Could not access files.");
}
output.flush();
output.close();
}
void comparefiles(String theServer,String oldServer,String theDay,String theHour, int oldHour) {
String thisServer = "";
if (theServer == oldServer) {
println("Same!");
sameServer = true;
} else {
println("Different!");
sameServer = false;
}
if (!sameServer) {
if ((oldHour != 23) && (oldHour != -1)) { // old server missed last hour(s)
thisServer = oldServer;
missed = " last hour(s) " + (oldHour+1) + " through 23";
} else {
if (int(theHour) != 0) { // new server missed first hour(s)
thisServer = theServer;
missed = " first hour(s) 0 through " + (int(theHour)-1);
}
}
} else {
if (int(theHour) != (oldHour + 1)) { // same server missed mid hour(s)
thisServer = theServer;
missed = " mid hour(s) " + (oldHour+1) + " through " + (int(theHour)-1);
}
}
if (thisServer != "") {
station = "1 |";
filestats(station,thisServer,theDay,missed);
}
}
void filestats(String station,String thisServer,String theDay,String missed) {
if (missed != "") {
println(station + thisServer +"|"+ theDay +"| missed |"+ missed);
missed = "";
}
}
void stats(String theDay,long fileCount) {
println();
println(theDay +" had |"+ fileCount + "| files");
}
void parsefilename(String filename) {
int p1 = filename.indexOf("-");
int p2 = filename.indexOf("0401");
theServer = filename.substring(0,p1);
theDay = filename.substring(p2,p2 + 4);
theHour = filename.substring(p2 + 5,p2 + 7);
}
1