943,608 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4839
  • Java RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Aug 4th, 2008
0

Re: EXCEL CSV FILES Handling

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--

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 4th, 2008
0

Re: EXCEL CSV FILES Handling

yes it does work... so i just need to apply this concept.. to your script???
Reputation Points: 7
Solved Threads: 0
Light Poster
jorgeflorencio is offline Offline
45 posts
since Apr 2008
Aug 4th, 2008
0

Re: EXCEL CSV FILES Handling

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 4th, 2008
0

Re: EXCEL CSV FILES Handling

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

Hi Mr. Alex!

Thank you in advance....

what i'm trying to do is....
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. if(inspectionResults.hasFailed(HEIGHT_AVG_RESULT)){
  2. Height = getAdjacentValue(HEIGHT_AVG_RESULT);

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

thank you!
Reputation Points: 7
Solved Threads: 0
Light Poster
jorgeflorencio is offline Offline
45 posts
since Apr 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

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--

java Syntax (Toggle Plain Text)
  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) --

java Syntax (Toggle Plain Text)
  1. AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

HI!

The first block i'd already define previously...
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. AdjacentValue = inspectionResults.getAdjacentValue(HEIGHT_AVG_RESULT);


thanks
Reputation Points: 7
Solved Threads: 0
Light Poster
jorgeflorencio is offline Offline
45 posts
since Apr 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

Notice that you are masking the upmost declared variables with newly defined ones in your if statements--

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

doesn't work...

Java Syntax (Toggle Plain Text)
  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. }}}
Reputation Points: 7
Solved Threads: 0
Light Poster
jorgeflorencio is offline Offline
45 posts
since Apr 2008
Aug 5th, 2008
0

Re: EXCEL CSV FILES Handling

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem with struts
Next Thread in Java Forum Timeline: Something about Graphics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC