How often do your lines vary in information? You could go about this a few ways
-Create classes to handle those cases
-Adapt the current class to simply handle all cases.
I'd go for number 2 to be safe, but in order to do that more information about what's varying will be needed.
HI..
The information will vary only with 44 and 32... so i'm adapting the code for 32 as well...
I'm going to try first.. and then will tell Further more.. THHHAANNNKKK YOOUUU SOO MUCH...
HI.. The information will vary only with 44 and 32... so i'm adapting the code for 32 as well...
I'm going to try first.. and then will tell Further more.. THHHAANNNKKK YOOUUU SOO MUCH...
For the final values declared in global scope of the class, you may additionally (or inversely) want to declare an array or enum that holds those values so you can simply iterate through them when processing your information.
I hope everything turns out well for you.
-Alex
Thank you for your help...
Eclipse is giving me an error on this line ¨
temp = Arrays.copyOfRange(temp, 1, temp.length);
in copyOfRange...
what library are you using?
Notice the import statements --
import java.io.*;
import java.util.Arrays;
/*from Arrays*/ static <T> T[] Arrays.copyOfRange( T[] array, int from, int size )
package spimonitoring;
import java.io.*;
import java.util.Arrays;
import java.lang.reflect.Array;
public class InspectionResults2{
public static final byte HEIGHT_AVG_RESULT = 7,
HEIGHT_RANGE_RESULT = 12,
AREA_AVG_RESULT = 15,
AREA_RANGE_RESULT = 20,
VOLUME_AVG_RESULT = 23,
VOLUME_RANGE_RESULT = 28,
HAV_FAILED_FEATURE_RESULT = 35,
REG_FAILED_FEATURE_RESULT = 38,
BRIDGE_FAILED_FEATURE_RESULT = 41;
private String retrievedData[];
private boolean failed[];
/**
* Constructs this InspectionResult with the data stored in the args.
* This class expects 44 values within the range of the args.
*/
public InspectionResults2(String... args){
retrievedData = args;
boolean temp[] ={
((retrievedData[7].equalsIgnoreCase("F")) ? true: false),
((retrievedData[12].equalsIgnoreCase("F")) ? true: false),
((retrievedData[15].equalsIgnoreCase("F")) ? true: false),
((retrievedData[20].equalsIgnoreCase("F")) ? true: false),
((retrievedData[23].equalsIgnoreCase("F")) ? true: false),
((retrievedData[28].equalsIgnoreCase("F")) ? true: false),
((retrievedData[35].equalsIgnoreCase("F")) ? true: false),
((retrievedData[38].equalsIgnoreCase("F")) ? true: false),
((retrievedData[41].equalsIgnoreCase("F")) ? true: false)
};
failed = temp;
}
public static void main(String... args){
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader(new File("INSPECT.txt"));
br = new BufferedReader(fr);
}catch(Exception e){e.printStackTrace();}
String dwArray[][] ={ {""}, {""}, {""} };
for(int i = 0; i < dwArray.length; i++){
String temp[] = null;
try{ temp = br.readLine().split(",");
}
catch(Exception f){f.printStackTrace();
System.exit(1);
};
temp = Arrays.<String>copyOfRange(temp, 1, temp.length);
//static <T> T[] Arrays.copyOfRange( T[] array, int from, int size )
dwArray[i] = temp;
}
InspectionResults2 ir[] =
{
new InspectionResults2(dwArray[0]),
new InspectionResults2(dwArray[1]),
new InspectionResults2(dwArray[2])
};
System.out.println(ir[0]); // as an example
spacer(3);
try{
System.out.println(ir[0].hasFailed(InspectionResults2.HEIGHT_AVG_RESULT));
System.out.println(ir[0].getAdjacentValue(InspectionResults2.HEIGHT_AVG_RESULT));
}catch(Exception e){
System.out.println(e);
}
try{
fr.close();
br.close();
}catch(Exception e){
}
}
private static void spacer(int lines){
for(int i = 0; i < lines; i++)
System.out.println();
}
/**
* Returns true if the given value has failed, returns false otherwise.
* It's preferred to use the constants defined within this class to get the
* desired information, and not regular ints.
*/
public boolean hasFailed(byte result) throws Exception{
switch(result){
case HEIGHT_AVG_RESULT:
return failed[0];
case HEIGHT_RANGE_RESULT:
return failed[1];
case AREA_AVG_RESULT:
return failed[2];
case AREA_RANGE_RESULT:
return failed[3];
case VOLUME_AVG_RESULT:
return failed[4];
case VOLUME_RANGE_RESULT:
return failed[5];
case HAV_FAILED_FEATURE_RESULT:
return failed[6];
case REG_FAILED_FEATURE_RESULT:
return failed[7];
case BRIDGE_FAILED_FEATURE_RESULT:
return failed[8];
default :
throw new Exception("Attempt to access invalid result type! Use the Result Constants to avoid this error!");
}
}
/**
* Returns the value next to the specified result.
*/
public String getAdjacentValue(byte result) throws Exception{
if(result >= 0 && result < retrievedData.length - 1)
return retrievedData[result + 1];
else throw new Exception("Error! Attempt to access column with either no adjacent value or outside of data-range!");
}
/**
* Simply returns a String representing the data for each value in this class.
*/
@Override
public String toString(){
String temp = "";
for(String element : retrievedData){
if(element.toString() != retrievedData[retrievedData.length - 1])
temp += element + ", ";
else temp += element;
}
return temp;
}
Something is wrong is giving me an error...
It may (or may not) have something to do with the Standard Edition of Java you are currently using.
What JDK are you currently using?
Notice the difference in these two links, for example.
SE 1.4.2 -- http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html
SE 6.0 -- http://java.sun.com/javase/6/docs/api/java/util/Arrays.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html
Apparently 1.5 doesn't support copyOfRange...
hmm I think this is doable--
import java.util.ArrayList;
public class MyArrays{
public static <T> T[] copyOfRange(T[] array, T[] emptyArray, int from, int size){
ArrayList<T> temp = new ArrayList<T>(0);
for(int i = from; i < size; i++){
temp.add(array[i]);
}
return temp.toArray(emptyArray);
}
public static void main(String... args){
String values[] = {"Tom", "Joe", "Sarah"};
String temp[] = {};
String result[] = MyArrays.<String>copyOfRange(values, temp, 1, values.length);
for(String element : result){
System.out.print(element + " ");
}
}
}yes it does work... so i just need to apply this concept.. to your script???
Yes, either supply the class MyArrays to the same directory as InspectionResults or provide MyArrays as an inner class to InspectionResults.
Then replace the incompatible code with my version and you should be set. (Remember my version requires that you additionally supply an empty array (not a null one, but an empty array of the same type)).
import java.io.*;
import java.util.ArrayList;
public class InspectionResults{
public static final byte HEIGHT_AVG_RESULT = 7,
HEIGHT_RANGE_RESULT = 12,
AREA_AVG_RESULT = 15,
AREA_RANGE_RESULT = 20,
VOLUME_AVG_RESULT = 23,
VOLUME_RANGE_RESULT = 28,
HAV_FAILED_FEATURE_RESULT = 35,
REG_FAILED_FEATURE_RESULT = 38,
BRIDGE_FAILED_FEATURE_RESULT = 41;
private String retrievedData[];
private boolean failed[];
/**
* Constructs this InspectionResult with the data stored in the args.
* This class expects 44 values within the range of the args.
*/
public InspectionResults(String... args){
retrievedData = args;
boolean temp[] ={
((retrievedData[7].equalsIgnoreCase("F")) ? true: false),
((retrievedData[12].equalsIgnoreCase("F")) ? true: false),
((retrievedData[15].equalsIgnoreCase("F")) ? true: false),
((retrievedData[20].equalsIgnoreCase("F")) ? true: false),
((retrievedData[23].equalsIgnoreCase("F")) ? true: false),
((retrievedData[28].equalsIgnoreCase("F")) ? true: false),
((retrievedData[35].equalsIgnoreCase("F")) ? true: false),
((retrievedData[38].equalsIgnoreCase("F")) ? true: false),
((retrievedData[41].equalsIgnoreCase("F")) ? true: false)
};
failed = temp;
}
static class MyArrays{
public static <T> T[] copyOfRange(T[] array, T[] emptyArray, int from, int size){
ArrayList<T> temp = new ArrayList<T>(0);
for(int i = from; i < size; i++){
temp.add(array[i]);
}
return temp.toArray(emptyArray);
}
}
public static void main(String... args){
FileReader fr = null;
BufferedReader br = null;
try{
fr = new FileReader(new File("INSPECT.txt"));
br = new BufferedReader(fr);
}catch(Exception e){e.printStackTrace();}
String dwArray[][] ={ {""}, {""}, {""} };
for(int i = 0; i < dwArray.length; i++){
String temp[] = null;
try{ temp = br.readLine().split(",");}catch(Exception f){f.printStackTrace(); System.exit(1);};
String empty[] = {};
temp = InspectionResults.MyArrays.<String>copyOfRange(temp, empty, 1, temp.length);
dwArray[i] = temp;
}
InspectionResults ir[] =
{
new InspectionResults(dwArray[0]),
new InspectionResults(dwArray[1]),
new InspectionResults(dwArray[2])
};
System.out.println(ir[0]); // as an example
spacer(3);
try{
System.out.println(ir[0].hasFailed(InspectionResults.HEIGHT_AVG_RESULT));
System.out.println(ir[0].getAdjacentValue(InspectionResults.HEIGHT_AVG_RESULT));
}catch(Exception e){
System.out.println(e);
}
try{
fr.close();
br.close();
}catch(Exception e){
}
}
private static void spacer(int lines){
for(int i = 0; i < lines; i++)
System.out.println();
}
/**
* Returns true if the given value has failed, returns false otherwise.
* It's preferred to use the constants defined within this class to get the
* desired information, and not regular ints.
*/
public boolean hasFailed(byte result) throws Exception{
switch(result){
case HEIGHT_AVG_RESULT:
return failed[0];
case HEIGHT_RANGE_RESULT:
return failed[1];
case AREA_AVG_RESULT:
return failed[2];
case AREA_RANGE_RESULT:
return failed[3];
case VOLUME_AVG_RESULT:
return failed[4];
case VOLUME_RANGE_RESULT:
return failed[5];
case HAV_FAILED_FEATURE_RESULT:
return failed[6];
case REG_FAILED_FEATURE_RESULT:
return failed[7];
case BRIDGE_FAILED_FEATURE_RESULT:
return failed[8];
default :
throw new Exception("Attempt to access invalid result type! Use the Result Constants to avoid this error!");
}
}
/**
* Returns the value next to the specified result.
*/
public String getAdjacentValue(byte result) throws Exception{
if(result >= 0 && result < retrievedData.length - 1)
return retrievedData[result + 1];
else throw new Exception("Error! Attempt to access column with either no adjacent value or outside of data-range!");
}
/**
* Simply returns a String representing the date for each value in this class.
*/
@Override
public String toString(){
String temp = "";
for(String element : retrievedData){
if(element.toString() != retrievedData[retrievedData.length - 1])
temp += element + ", ";
else temp += element;
}
return temp;
}
}Hi Mr. Alex!
Thank you in advance....
what i'm trying to do is....
public void listRecursively(File fdir, int depth) throws IOException {
/*Transform milliseconds time to gregorian time */
long datefiles = fdir.lastModified();
SimpleDateFormat Date = new SimpleDateFormat (" dd/MM/yyyy , HH:mm:ss aaa");
Date nDate = new Date(datefiles);
String F = ",F,";
int count = 0;
/*Line counter*/
try
{
RandomAccessFile File = new RandomAccessFile(fdir,"r");
long lastline=File.length();
File.close();
FileReader fileRead = new FileReader(fdir);
BufferedReader bufferReader = new BufferedReader(fileRead);
Scanner scan = new Scanner(fdir);
while(scan.hasNextLine()){
// if(scan.nextLine().contains(F))
// count++;
String linha = scan.nextLine();
if (linha.contains(F))
count++;
InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
if (linha.split(",").length == 32){
byte HEIGHT_AVG_RESULT = 6,
AREA_AVG_RESULT = 11,
VOLUME_AVG_RESULT = 16,
REG_OFF_RESULT = 22,
BRIDGE_LEN_RESULT = 29;
} else{//File with 44
byte HEIGHT_AVG_RESULT = 7,
HEIGHT_RANGE_RESULT = 12,
AREA_AVG_RESULT = 15,
AREA_RANGE_RESULT = 20,
VOLUME_AVG_RESULT = 23,
VOLUME_RANGE_RESULT = 28,
HAV_FAILED_FEATURE_RESULT = 35,
REG_OFF_RESULT = 38,
BRIDGE_LEN_RESULT = 41;
}
if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){// faz isso para todas as variáveis.
AdjacentValue = getAdjacentValue(HEIGHT_AVG_RESULT);
}
}
fileRead.close();
bufferReader.close();
// /*Start Line counter mode2*/
// LineNumberReader lineRead = new LineNumberReader(fileRead);
// lineRead.skip(lastline);
// int countline = lineRead.getLineNumber()-6; //number of default lines = 6
// fileRead.close();
// lineRead.close();
// /*End Line counter mode2*/
/* Output1 */
if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
System.out.println(INDENTS[depth] + x +", "+fdir.getName() +", "+ count +","+Date.format(nDate));
/* Output2 */
if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
fw.write( INDENTS[depth] + x +", "+ fdir.getName() +", "+ count +","+ Date.format(nDate)+ System.getProperty("line.separator"));
fw.flush();
//if (fdir.getPath().endsWith(".csv") && (fdir.length()/512 )>= 1 && fdir.length()/512 <= 3)
}
catch(IOException e){
}
if (fdir.isDirectory() && !fdir.isHidden() && depth < MAX_DEPTH) {
for (File f : fdir.listFiles()){ // Go over each file/subdirectory.
listRecursively(f, depth+1);
}}}
but something is not flowing as i intend...
in this part
if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){
Height = getAdjacentValue(HEIGHT_AVG_RESULT); why i can't retrieve the value like this...??
thank you!
You'll want to declare the numbers before the if blocks if you want them to be visible to the rest of the values in the try block--
InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
byte HEIGHT_AVG_RESULT = 0,
HEIGHT_RANGE_RESULT = 0,
AREA_AVG_RESULT = 0,
AREA_RANGE_RESULT = 0,
VOLUME_AVG_RESULT = 0,
VOLUME_RANGE_RESULT = 0,
HAV_FAILED_FEATURE_RESULT = 0,
REG_OFF_RESULT = 0,
BRIDGE_LEN_RESULT = 0;
if (linha.split(",").length == 32){
HEIGHT_AVG_RESULT = 6,
AREA_AVG_RESULT = 11,
VOLUME_AVG_RESULT = 16,
REG_OFF_RESULT = 22,
BRIDGE_LEN_RESULT = 29;
} else{//File with 44
HEIGHT_AVG_RESULT = 7,
HEIGHT_RANGE_RESULT = 12,
AREA_AVG_RESULT = 15,
AREA_RANGE_RESULT = 20,
VOLUME_AVG_RESULT = 23,
VOLUME_RANGE_RESULT = 28,
HAV_FAILED_FEATURE_RESULT = 35,
REG_OFF_RESULT = 38,
BRIDGE_LEN_RESULT = 41;
}
if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){// faz isso para todas as variáveis.
AdjacentValue = getAdjacentValue(HEIGHT_AVG_RESULT);
--Also don't forget to use the appropriate InspectionResults44 object with the call to getAdjacentValue(HEIGHT_AVG_RESULT) --
AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);HI!
The first block i'd already define previously...
byte HEIGHT_AVG_RESULT = 0,
HEIGHT_RANGE_RESULT = 0,
AREA_AVG_RESULT = 0,
AREA_RANGE_RESULT = 0,
VOLUME_AVG_RESULT = 0,
VOLUME_RANGE_RESULT = 0,
HAV_FAILED_FEATURE_RESULT = 0,
REG_OFF_RESULT = 0,
BRIDGE_LEN_RESULT = 0;
my problem is with
AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT); thanks
Notice that you are masking the upmost declared variables with newly defined ones in your if statements--
if (linha.split(",").length == 32){
// values already defined, get rid of "byte"
/* byte*/ HEIGHT_AVG_RESULT = 6,
AREA_AVG_RESULT = 11,
VOLUME_AVG_RESULT = 16,
REG_OFF_RESULT = 22,
BRIDGE_LEN_RESULT = 29;
} else{//File with 44
// values already defined, get rid of "byte"
/*byte*/ HEIGHT_AVG_RESULT = 7,
HEIGHT_RANGE_RESULT = 12,
AREA_AVG_RESULT = 15,
AREA_RANGE_RESULT = 20,
VOLUME_AVG_RESULT = 23,
VOLUME_RANGE_RESULT = 28,
HAV_FAILED_FEATURE_RESULT = 35,
REG_OFF_RESULT = 38,
BRIDGE_LEN_RESULT = 41;
}doesn't work...:(
public void listRecursively(File fdir, int depth) throws IOException {
/*Transform milliseconds time to gregorian time */
long datefiles = fdir.lastModified();
SimpleDateFormat Date = new SimpleDateFormat (" dd/MM/yyyy , HH:mm:ss aaa");
Date nDate = new Date(datefiles);
String F = ",F,";
int count = 0;
/*Line counter*/
try
{
RandomAccessFile File = new RandomAccessFile(fdir,"r");
long lastline=File.length();
File.close();
FileReader fileRead = new FileReader(fdir);
BufferedReader bufferReader = new BufferedReader(fileRead);
Scanner scan = new Scanner(fdir);
while(scan.hasNextLine()){
// if(scan.nextLine().contains(F))
// count++;
String linha = scan.nextLine();
if (linha.contains(F))
count++;
InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
if (linha.split(",").length == 32){
HEIGHT_AVG_RESULT = 6;
AREA_AVG_RESULT = 11;
VOLUME_AVG_RESULT = 16;
REG_OFF_RESULT = 22;
BRIDGE_LEN_RESULT = 29;
} else{//File with 44
HEIGHT_AVG_RESULT = 7;
HEIGHT_RANGE_RESULT = 12;
AREA_AVG_RESULT = 15;
AREA_RANGE_RESULT = 20;
VOLUME_AVG_RESULT = 23;
VOLUME_RANGE_RESULT = 28;
HAV_FAILED_FEATURE_RESULT = 35;
REG_OFF_RESULT = 38;
BRIDGE_LEN_RESULT = 41;
}
if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){
adjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
}
}
fileRead.close();
bufferReader.close();
// /*Start Line counter mode2*/
// LineNumberReader lineRead = new LineNumberReader(fileRead);
// lineRead.skip(lastline);
// int countline = lineRead.getLineNumber()-6; //number of default lines = 6
// fileRead.close();
// lineRead.close();
// /*End Line counter mode2*/
/* Output1 */
if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
System.out.println(INDENTS[depth] + x +", "+fdir.getName() +", "+ count +","+Date.format(nDate));
/* Output2 */
if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
fw.write( INDENTS[depth] + x +", "+ fdir.getName() +", "+ count +","+ Date.format(nDate)+ System.getProperty("line.separator"));
fw.flush();
//if (fdir.getPath().endsWith(".csv") && (fdir.length()/512 )>= 1 && fdir.length()/512 <= 3)
}
catch(IOException e){
}
if (fdir.isDirectory() && !fdir.isHidden() && depth < MAX_DEPTH) {
for (File f : fdir.listFiles()){ // Go over each file/subdirectory.
listRecursively(f, depth+1);
}}}Where in your code did you place those variables?
They need to be visible to the method that is being called - either through global scope or within the same scope as the method.