EXCEL CSV FILES Handling

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #31
Aug 4th, 2008
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--

  1. import java.util.ArrayList;
  2.  
  3. public class MyArrays{
  4.  
  5.  
  6. public static <T> T[] copyOfRange(T[] array, T[] emptyArray, int from, int size){
  7. ArrayList<T> temp = new ArrayList<T>(0);
  8. for(int i = from; i < size; i++){
  9. temp.add(array[i]);
  10. }
  11.  
  12. return temp.toArray(emptyArray);
  13. }
  14.  
  15. public static void main(String... args){
  16.  
  17. String values[] = {"Tom", "Joe", "Sarah"};
  18. String temp[] = {};
  19.  
  20. String result[] = MyArrays.<String>copyOfRange(values, temp, 1, values.length);
  21.  
  22. for(String element : result){
  23. System.out.print(element + " ");
  24. }
  25.  
  26. }
  27.  
  28. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: EXCEL CSV FILES Handling

 
0
  #32
Aug 4th, 2008
yes it does work... so i just need to apply this concept.. to your script???
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #33
Aug 4th, 2008
Originally Posted by jorgeflorencio View Post
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)).
Last edited by Alex Edwards; Aug 4th, 2008 at 1:31 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #34
Aug 4th, 2008
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class InspectionResults{
  5.  
  6. public static final byte HEIGHT_AVG_RESULT = 7,
  7. HEIGHT_RANGE_RESULT = 12,
  8. AREA_AVG_RESULT = 15,
  9. AREA_RANGE_RESULT = 20,
  10. VOLUME_AVG_RESULT = 23,
  11. VOLUME_RANGE_RESULT = 28,
  12. HAV_FAILED_FEATURE_RESULT = 35,
  13. REG_FAILED_FEATURE_RESULT = 38,
  14. BRIDGE_FAILED_FEATURE_RESULT = 41;
  15. private String retrievedData[];
  16. private boolean failed[];
  17.  
  18. /**
  19. * Constructs this InspectionResult with the data stored in the args.
  20. * This class expects 44 values within the range of the args.
  21. */
  22. public InspectionResults(String... args){
  23. retrievedData = args;
  24. boolean temp[] ={
  25. ((retrievedData[7].equalsIgnoreCase("F")) ? true: false),
  26. ((retrievedData[12].equalsIgnoreCase("F")) ? true: false),
  27. ((retrievedData[15].equalsIgnoreCase("F")) ? true: false),
  28. ((retrievedData[20].equalsIgnoreCase("F")) ? true: false),
  29. ((retrievedData[23].equalsIgnoreCase("F")) ? true: false),
  30. ((retrievedData[28].equalsIgnoreCase("F")) ? true: false),
  31. ((retrievedData[35].equalsIgnoreCase("F")) ? true: false),
  32. ((retrievedData[38].equalsIgnoreCase("F")) ? true: false),
  33. ((retrievedData[41].equalsIgnoreCase("F")) ? true: false)
  34. };
  35. failed = temp;
  36. }
  37.  
  38. static class MyArrays{
  39. public static <T> T[] copyOfRange(T[] array, T[] emptyArray, int from, int size){
  40. ArrayList<T> temp = new ArrayList<T>(0);
  41. for(int i = from; i < size; i++){
  42. temp.add(array[i]);
  43. }
  44.  
  45. return temp.toArray(emptyArray);
  46. }
  47. }
  48.  
  49. public static void main(String... args){
  50.  
  51. FileReader fr = null;
  52. BufferedReader br = null;
  53. try{
  54. fr = new FileReader(new File("INSPECT.txt"));
  55. br = new BufferedReader(fr);
  56. }catch(Exception e){e.printStackTrace();}
  57.  
  58.  
  59. String dwArray[][] ={ {""}, {""}, {""} };
  60.  
  61. for(int i = 0; i < dwArray.length; i++){
  62. String temp[] = null;
  63.  
  64. try{ temp = br.readLine().split(",");}catch(Exception f){f.printStackTrace(); System.exit(1);};
  65. String empty[] = {};
  66. temp = InspectionResults.MyArrays.<String>copyOfRange(temp, empty, 1, temp.length);
  67. dwArray[i] = temp;
  68. }
  69.  
  70.  
  71. InspectionResults ir[] =
  72. {
  73. new InspectionResults(dwArray[0]),
  74. new InspectionResults(dwArray[1]),
  75. new InspectionResults(dwArray[2])
  76. };
  77.  
  78. System.out.println(ir[0]); // as an example
  79. spacer(3);
  80.  
  81. try{
  82. System.out.println(ir[0].hasFailed(InspectionResults.HEIGHT_AVG_RESULT));
  83. System.out.println(ir[0].getAdjacentValue(InspectionResults.HEIGHT_AVG_RESULT));
  84. }catch(Exception e){
  85. System.out.println(e);
  86. }
  87.  
  88. try{
  89. fr.close();
  90. br.close();
  91. }catch(Exception e){
  92. }
  93. }
  94.  
  95. private static void spacer(int lines){
  96. for(int i = 0; i < lines; i++)
  97. System.out.println();
  98. }
  99.  
  100. /**
  101. * Returns true if the given value has failed, returns false otherwise.
  102. * It's preferred to use the constants defined within this class to get the
  103. * desired information, and not regular ints.
  104. */
  105. public boolean hasFailed(byte result) throws Exception{
  106. switch(result){
  107. case HEIGHT_AVG_RESULT:
  108. return failed[0];
  109. case HEIGHT_RANGE_RESULT:
  110. return failed[1];
  111. case AREA_AVG_RESULT:
  112. return failed[2];
  113. case AREA_RANGE_RESULT:
  114. return failed[3];
  115. case VOLUME_AVG_RESULT:
  116. return failed[4];
  117. case VOLUME_RANGE_RESULT:
  118. return failed[5];
  119. case HAV_FAILED_FEATURE_RESULT:
  120. return failed[6];
  121. case REG_FAILED_FEATURE_RESULT:
  122. return failed[7];
  123. case BRIDGE_FAILED_FEATURE_RESULT:
  124. return failed[8];
  125. default :
  126. throw new Exception("Attempt to access invalid result type! Use the Result Constants to avoid this error!");
  127. }
  128. }
  129.  
  130. /**
  131. * Returns the value next to the specified result.
  132. */
  133. public String getAdjacentValue(byte result) throws Exception{
  134. if(result >= 0 && result < retrievedData.length - 1)
  135. return retrievedData[result + 1];
  136. else throw new Exception("Error! Attempt to access column with either no adjacent value or outside of data-range!");
  137. }
  138.  
  139. /**
  140. * Simply returns a String representing the date for each value in this class.
  141. */
  142. @Override
  143. public String toString(){
  144. String temp = "";
  145. for(String element : retrievedData){
  146. if(element.toString() != retrievedData[retrievedData.length - 1])
  147. temp += element + ", ";
  148. else temp += element;
  149. }
  150. return temp;
  151. }
  152. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: EXCEL CSV FILES Handling

 
0
  #35
Aug 5th, 2008
Hi Mr. Alex!

Thank you in advance....

what i'm trying to do is....
  1. public void listRecursively(File fdir, int depth) throws IOException {
  2.  
  3. /*Transform milliseconds time to gregorian time */
  4. long datefiles = fdir.lastModified();
  5. SimpleDateFormat Date = new SimpleDateFormat (" dd/MM/yyyy , HH:mm:ss aaa");
  6. Date nDate = new Date(datefiles);
  7.  
  8. String F = ",F,";
  9. int count = 0;
  10.  
  11. /*Line counter*/
  12. try
  13. {
  14. RandomAccessFile File = new RandomAccessFile(fdir,"r");
  15. long lastline=File.length();
  16. File.close();
  17.  
  18. FileReader fileRead = new FileReader(fdir);
  19. BufferedReader bufferReader = new BufferedReader(fileRead);
  20.  
  21. Scanner scan = new Scanner(fdir);
  22. while(scan.hasNextLine()){
  23.  
  24. // if(scan.nextLine().contains(F))
  25. // count++;
  26. String linha = scan.nextLine();
  27. if (linha.contains(F))
  28. count++;
  29. InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
  30.  
  31. if (linha.split(",").length == 32){
  32.  
  33. byte HEIGHT_AVG_RESULT = 6,
  34. AREA_AVG_RESULT = 11,
  35. VOLUME_AVG_RESULT = 16,
  36. REG_OFF_RESULT = 22,
  37. BRIDGE_LEN_RESULT = 29;
  38.  
  39. } else{//File with 44
  40.  
  41. byte HEIGHT_AVG_RESULT = 7,
  42. HEIGHT_RANGE_RESULT = 12,
  43. AREA_AVG_RESULT = 15,
  44. AREA_RANGE_RESULT = 20,
  45. VOLUME_AVG_RESULT = 23,
  46. VOLUME_RANGE_RESULT = 28,
  47. HAV_FAILED_FEATURE_RESULT = 35,
  48. REG_OFF_RESULT = 38,
  49. BRIDGE_LEN_RESULT = 41;
  50. }
  51.  
  52. if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){// faz isso para todas as variáveis.
  53. AdjacentValue = getAdjacentValue(HEIGHT_AVG_RESULT);
  54.  
  55. }
  56. }
  57.  
  58. fileRead.close();
  59. bufferReader.close();
  60.  
  61. // /*Start Line counter mode2*/
  62. // LineNumberReader lineRead = new LineNumberReader(fileRead);
  63. // lineRead.skip(lastline);
  64. // int countline = lineRead.getLineNumber()-6; //number of default lines = 6
  65. // fileRead.close();
  66. // lineRead.close();
  67. // /*End Line counter mode2*/
  68.  
  69. /* Output1 */
  70. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  71. System.out.println(INDENTS[depth] + x +", "+fdir.getName() +", "+ count +","+Date.format(nDate));
  72. /* Output2 */
  73. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  74. fw.write( INDENTS[depth] + x +", "+ fdir.getName() +", "+ count +","+ Date.format(nDate)+ System.getProperty("line.separator"));
  75. fw.flush();
  76. //if (fdir.getPath().endsWith(".csv") && (fdir.length()/512 )>= 1 && fdir.length()/512 <= 3)
  77. }
  78. catch(IOException e){
  79. }
  80. if (fdir.isDirectory() && !fdir.isHidden() && depth < MAX_DEPTH) {
  81. for (File f : fdir.listFiles()){ // Go over each file/subdirectory.
  82. listRecursively(f, depth+1);
  83. }}}
but something is not flowing as i intend...

in this part
  1. if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){
  2. Height = getAdjacentValue(HEIGHT_AVG_RESULT);

why i can't retrieve the value like this...??

thank you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #36
Aug 5th, 2008
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--

  1. InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
  2.  
  3.  
  4. byte HEIGHT_AVG_RESULT = 0,
  5. HEIGHT_RANGE_RESULT = 0,
  6. AREA_AVG_RESULT = 0,
  7. AREA_RANGE_RESULT = 0,
  8. VOLUME_AVG_RESULT = 0,
  9. VOLUME_RANGE_RESULT = 0,
  10. HAV_FAILED_FEATURE_RESULT = 0,
  11. REG_OFF_RESULT = 0,
  12. BRIDGE_LEN_RESULT = 0;
  13.  
  14.  
  15.  
  16. if (linha.split(",").length == 32){
  17.  
  18. HEIGHT_AVG_RESULT = 6,
  19. AREA_AVG_RESULT = 11,
  20. VOLUME_AVG_RESULT = 16,
  21. REG_OFF_RESULT = 22,
  22. BRIDGE_LEN_RESULT = 29;
  23.  
  24. } else{//File with 44
  25.  
  26. HEIGHT_AVG_RESULT = 7,
  27. HEIGHT_RANGE_RESULT = 12,
  28. AREA_AVG_RESULT = 15,
  29. AREA_RANGE_RESULT = 20,
  30. VOLUME_AVG_RESULT = 23,
  31. VOLUME_RANGE_RESULT = 28,
  32. HAV_FAILED_FEATURE_RESULT = 35,
  33. REG_OFF_RESULT = 38,
  34. BRIDGE_LEN_RESULT = 41;
  35. }
  36.  
  37. if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){// faz isso para todas as variáveis.
  38. AdjacentValue = getAdjacentValue(HEIGHT_AVG_RESULT);

--Also don't forget to use the appropriate InspectionResults44 object with the call to getAdjacentValue(HEIGHT_AVG_RESULT) --

  1. AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: EXCEL CSV FILES Handling

 
0
  #37
Aug 5th, 2008
HI!

The first block i'd already define previously...
  1. byte HEIGHT_AVG_RESULT = 0,
  2. HEIGHT_RANGE_RESULT = 0,
  3. AREA_AVG_RESULT = 0,
  4. AREA_RANGE_RESULT = 0,
  5. VOLUME_AVG_RESULT = 0,
  6. VOLUME_RANGE_RESULT = 0,
  7. HAV_FAILED_FEATURE_RESULT = 0,
  8. REG_OFF_RESULT = 0,
  9. BRIDGE_LEN_RESULT = 0;

my problem is with
  1. AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);


thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #38
Aug 5th, 2008
Notice that you are masking the upmost declared variables with newly defined ones in your if statements--

  1. if (linha.split(",").length == 32){
  2.  
  3. // values already defined, get rid of "byte"
  4. /* byte*/ HEIGHT_AVG_RESULT = 6,
  5. AREA_AVG_RESULT = 11,
  6. VOLUME_AVG_RESULT = 16,
  7. REG_OFF_RESULT = 22,
  8. BRIDGE_LEN_RESULT = 29;
  9.  
  10. } else{//File with 44
  11.  
  12. // values already defined, get rid of "byte"
  13. /*byte*/ HEIGHT_AVG_RESULT = 7,
  14. HEIGHT_RANGE_RESULT = 12,
  15. AREA_AVG_RESULT = 15,
  16. AREA_RANGE_RESULT = 20,
  17. VOLUME_AVG_RESULT = 23,
  18. VOLUME_RANGE_RESULT = 28,
  19. HAV_FAILED_FEATURE_RESULT = 35,
  20. REG_OFF_RESULT = 38,
  21. BRIDGE_LEN_RESULT = 41;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: EXCEL CSV FILES Handling

 
0
  #39
Aug 5th, 2008
doesn't work...

  1. public void listRecursively(File fdir, int depth) throws IOException {
  2.  
  3. /*Transform milliseconds time to gregorian time */
  4. long datefiles = fdir.lastModified();
  5. SimpleDateFormat Date = new SimpleDateFormat (" dd/MM/yyyy , HH:mm:ss aaa");
  6. Date nDate = new Date(datefiles);
  7.  
  8. String F = ",F,";
  9. int count = 0;
  10.  
  11. /*Line counter*/
  12. try
  13. {
  14. RandomAccessFile File = new RandomAccessFile(fdir,"r");
  15. long lastline=File.length();
  16. File.close();
  17.  
  18. FileReader fileRead = new FileReader(fdir);
  19. BufferedReader bufferReader = new BufferedReader(fileRead);
  20.  
  21. Scanner scan = new Scanner(fdir);
  22. while(scan.hasNextLine()){
  23.  
  24. // if(scan.nextLine().contains(F))
  25. // count++;
  26. String linha = scan.nextLine();
  27. if (linha.contains(F))
  28. count++;
  29. InspectionResults44 inspectionResults = new InspectionResults44(linha.split(","));
  30.  
  31. if (linha.split(",").length == 32){
  32.  
  33. HEIGHT_AVG_RESULT = 6;
  34. AREA_AVG_RESULT = 11;
  35. VOLUME_AVG_RESULT = 16;
  36. REG_OFF_RESULT = 22;
  37. BRIDGE_LEN_RESULT = 29;
  38.  
  39. } else{//File with 44
  40.  
  41. HEIGHT_AVG_RESULT = 7;
  42. HEIGHT_RANGE_RESULT = 12;
  43. AREA_AVG_RESULT = 15;
  44. AREA_RANGE_RESULT = 20;
  45. VOLUME_AVG_RESULT = 23;
  46. VOLUME_RANGE_RESULT = 28;
  47. HAV_FAILED_FEATURE_RESULT = 35;
  48. REG_OFF_RESULT = 38;
  49. BRIDGE_LEN_RESULT = 41;
  50. }
  51.  
  52. if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){
  53. adjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
  54. }
  55. }
  56.  
  57. fileRead.close();
  58. bufferReader.close();
  59.  
  60. // /*Start Line counter mode2*/
  61. // LineNumberReader lineRead = new LineNumberReader(fileRead);
  62. // lineRead.skip(lastline);
  63. // int countline = lineRead.getLineNumber()-6; //number of default lines = 6
  64. // fileRead.close();
  65. // lineRead.close();
  66. // /*End Line counter mode2*/
  67.  
  68. /* Output1 */
  69. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  70. System.out.println(INDENTS[depth] + x +", "+fdir.getName() +", "+ count +","+Date.format(nDate));
  71. /* Output2 */
  72. if (fdir.getPath().endsWith(".csv") /*&& fdir.lastModified() > HostFile.lastModified()*/)
  73. fw.write( INDENTS[depth] + x +", "+ fdir.getName() +", "+ count +","+ Date.format(nDate)+ System.getProperty("line.separator"));
  74. fw.flush();
  75. //if (fdir.getPath().endsWith(".csv") && (fdir.length()/512 )>= 1 && fdir.length()/512 <= 3)
  76. }
  77. catch(IOException e){
  78. }
  79. if (fdir.isDirectory() && !fdir.isHidden() && depth < MAX_DEPTH) {
  80. for (File f : fdir.listFiles()){ // Go over each file/subdirectory.
  81. listRecursively(f, depth+1);
  82. }}}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: EXCEL CSV FILES Handling

 
0
  #40
Aug 5th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC