| | |
EXCEL CSV FILES Handling
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
http://java.sun.com/j2se/1.5.0/docs/...il/Arrays.html
Apparently 1.5 doesn't support copyOfRange...
hmm I think this is doable--
Apparently 1.5 doesn't support copyOfRange...
hmm I think this is doable--
java Syntax (Toggle Plain Text)
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???
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)).
Last edited by Alex Edwards; Aug 4th, 2008 at 1:31 am.
java Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Apr 2008
Posts: 45
Reputation:
Solved Threads: 0
Hi Mr. Alex!
Thank you in advance....
what i'm trying to do is.... but something is not flowing as i intend...
in this part
why i can't retrieve the value like this...??
thank you!
Thank you in advance....
what i'm trying to do is....
Java Syntax (Toggle Plain Text)
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); }}}
in this part
Java Syntax (Toggle Plain Text)
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--
--Also don't forget to use the appropriate InspectionResults44 object with the call to getAdjacentValue(HEIGHT_AVG_RESULT) --
java Syntax (Toggle Plain Text)
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) --
java Syntax (Toggle Plain Text)
AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
•
•
Join Date: Apr 2008
Posts: 45
Reputation:
Solved Threads: 0
HI!
The first block i'd already define previously...
my problem is with
thanks
The first block i'd already define previously...
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
thanks
Notice that you are masking the upmost declared variables with newly defined ones in your if statements--
java Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Apr 2008
Posts: 45
Reputation:
Solved Threads: 0
doesn't work...

Java Syntax (Toggle Plain Text)
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); }}}
![]() |
Other Threads in the Java Forum
- Previous Thread: Problem with struts
- Next Thread: Something about Graphics
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui html ide image input integer intersect iphone j2me java java.xls javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





