Posted By


trusktr on 10/17/10

Tagged


Statistics


Viewed 679 times
Favorited by 0 user(s)

cisp401 DataExtractor.java


/ Published in: Java
Save to your folder(s)

A custom class for retrieving deliminated data from a (custom) MyString string.


Copy this code and paste it in your HTML
  1. public class DataExtractor {
  2.  
  3. // Data Objects (
  4. private MyString data;
  5. private MyString delim; // The specified deliminator
  6. private int[] delims; // An array that will hold the positions of deliminators within a string of data.
  7. // )
  8. // Constructors (
  9. public DataExtractor() { // Default Constructor
  10. this(new MyString(), new MyString(" "), new int[]{0});
  11. }
  12. public DataExtractor(String s) { // Parameterized Constructor
  13. this(new MyString(s), new MyString(" "), new int[]{0});
  14. }
  15. public DataExtractor(String s, String d) { // Parameterized Constructor
  16. this(s, d, new int[]{0});
  17. }
  18. public DataExtractor(String s, String d, int[] dlms) { // Parameterized Constructor
  19. this(new MyString(s), new MyString(d), dlms);
  20. }
  21. public DataExtractor(MyString s) { // Parameterized Constructor
  22. this(s, new MyString(" "), new int[]{0});
  23. }
  24. public DataExtractor(MyString s, MyString d) { // Parameterized Constructor
  25. this(s, d, new int[]{0});
  26. }
  27. public DataExtractor(MyString s, MyString d, int[] dlms) { // Parameterized Constructor Template
  28. data = new MyString(s);
  29. delim = new MyString(d);
  30. delims = dlms;
  31. }
  32. public DataExtractor(DataExtractor d) { // Copy Constructor
  33. this(d.getData(), d.getDelim(), d.getDelims());
  34. }
  35. // )
  36. // Accessors (
  37. private MyString getData() {
  38. return new MyString(data);
  39. }
  40. private MyString getDelim() {
  41. return new MyString(delim);
  42. }
  43. private int[] getDelims() {
  44. return delims;
  45. }
  46. // )
  47. // Mutators (
  48. public void setBuffer(MyString d) {
  49. delim = new MyString(d);
  50. try {
  51. delims = data.findAll(delim.toString()); // The findAll method of my MyString class ended up coming very much in handy!!
  52. }
  53. delims = new int[]{0};
  54. System.out.println("Error: That deliminator was not found in the data.");
  55. }
  56. }
  57. public void setBuffer(String d) {
  58. this.setBuffer(new MyString(d));
  59. }
  60. public void setBuffer() {
  61. this.setBuffer(new MyString(" "));
  62. }
  63. public MyString get(int n) { // This assumes the client will know of what type the desired piece of data is to convert it from a String later.
  64. char[] itemChars = null;
  65. if ( n==0 ) {
  66. itemChars = new char[delims[n]];
  67. for (int i=0; i<delims[n]; i++) {
  68. itemChars[i] = data.get(i);
  69. }
  70. }
  71. if ( n>0 && n<delims.length ) {
  72. itemChars = new char[(delims[n] - delims[n-1]) - 1]; // The size of the piece of data is (delims[n+1]-delims[n])-1
  73. for (int i=delims[n-1]+1; i<delims[n]; i++) { // First character of the nth piece of data is at data.get(delims[n-1]+1) and last character is at data.get(delims[n]-1)
  74. itemChars[i-(delims[n-1]+1)] = data.get(i);
  75. }
  76. }
  77. if ( n == delims.length ) {
  78. itemChars = new char[(data.length() - delims[n-1]) - 1];
  79. for (int i=delims[n-1]+1; i<data.length(); i++) {
  80. itemChars[i-(delims[n-1]+1)] = data.get(i);
  81. }
  82. }
  83. return new MyString(itemChars);
  84. }
  85. // )
  86. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.