Return to Snippet

Revision: 34104
at November 19, 2010 20:15 by trusktr


Updated Code
public class DataExtractor {
	
	// Data Objects (
		private MyString data;
		private MyString delim; // The specified deliminator
		private int[] delims; // An array that will hold the positions of deliminators within a string of data.
//  )
	// Constructors (
		public DataExtractor() { // Default Constructor
			this(new MyString(), new MyString(" "), new int[]{0});
		}
		public DataExtractor(String s) { // Parameterized Constructor
			this(new MyString(s), new MyString(" "), new int[]{0});
		}
		public DataExtractor(String s, String d) { // Parameterized Constructor
			this(s, d, new int[]{0});
		}
		public DataExtractor(String s, String d, int[] dlms) { // Parameterized Constructor
			this(new MyString(s), new MyString(d), dlms);
		}
		public DataExtractor(MyString s) { // Parameterized Constructor
			this(s, new MyString(" "), new int[]{0});
		}
		public DataExtractor(MyString s, MyString d) { // Parameterized Constructor
			this(s, d, new int[]{0});
		}
		public DataExtractor(MyString s, MyString d, int[] dlms) { // Parameterized Constructor Template
			data = new MyString(s);
			delim = new MyString(d);
			delims = dlms;
		}
		public DataExtractor(DataExtractor d) { // Copy Constructor
			this(d.getData(), d.getDelim(), d.getDelims());
		}
//  )
	// Accessors (
		private MyString getData() {
			return new MyString(data);
		}
		private MyString getDelim() {
			return new MyString(delim);
		}
		private int[] getDelims() {
			return delims;
		}
//  )
	// Mutators (
		public void setBuffer(MyString d) {
			delim = new MyString(d);
			try {
				delims = data.findAll(delim.toString()); // The findAll method of my MyString class ended up coming very much in handy!!
			}
			catch (ArrayIndexOutOfBoundsException i) {
				delims = new int[]{0};
				System.out.println("Error: That deliminator was not found in the data.");
			}
		}
		public void setBuffer(String d) {
			this.setBuffer(new MyString(d));
		}
		public void setBuffer() {
			this.setBuffer(new MyString(" "));
		}
		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.
			char[] itemChars = null;
			if ( n==0 ) {
				itemChars = new char[delims[n]];
				for (int i=0; i<delims[n]; i++) {
					itemChars[i] = data.get(i);
				}
			}
			if ( n>0 && n<delims.length ) {
				itemChars = new char[(delims[n] - delims[n-1]) - 1]; // The size of the piece of data is (delims[n+1]-delims[n])-1
				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)
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			if ( n == delims.length ) {
				itemChars = new char[(data.length() - delims[n-1]) - 1];
				for (int i=delims[n-1]+1; i<data.length(); i++) {
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			return new MyString(itemChars);
		}
//  )
}

Revision: 34103
at October 17, 2010 15:24 by trusktr


Updated Code
public class DataReader {
	
	// Data Objects (
		private MyString data;
		private MyString delim; // The specified deliminator
		private int[] delims; // An array that will hold the positions of deliminators within a string of data.
//  )
	// Constructors (
		public DataReader() { // Default Constructor
			this(new MyString(), new MyString(" "), new int[]{0});
		}
		public DataReader(MyString s, MyString d, int[] dlms) { // Parameterized Constructor Template
			data = new MyString(s);
			delim = new MyString(d);
			delims = dlms;
		}
		public DataReader(MyString s) { // Parameterized Constructor
			this(s, new MyString(" "), new int[]{0});
		}
		public DataReader(String s, String d) { // Parameterized Constructor
			this(new MyString(s), new MyString(d), new int[]{0});
		}
		public DataReader(String s) { // Parameterized Constructor
			this(new MyString(s), new MyString(" "), new int[]{0});
		}
		public DataReader(DataReader d) { // Copy Constructor
			this(d.getData(), d.getDelim(), d.getDelims());
		}
//  )
	// Accessors (
		private MyString getData() {
			return new MyString(data);
		}
		private MyString getDelim() {
			return new MyString(delim);
		}
		private int[] getDelims() {
			return delims;
		}
//  )
	// Mutators (
		public void setBuffer(MyString d) {
			delim = new MyString(d);
			try {
				delims = data.findAll(delim.toString()); // The findAll method of my MyString class ended up coming very much in handy!!
			}
			catch (ArrayIndexOutOfBoundsException i) {
				delims = new int[]{0};
				System.out.println("Error: That deliminator was not found in the data.");
			}
		}
		public void setBuffer(String d) {
			this.setBuffer(new MyString(d));
		}
		public void setBuffer() {
			this.setBuffer(new MyString(" "));
		}
		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.
			char[] itemChars = null;
			if ( n==0 ) {
				itemChars = new char[delims[n]];
				for (int i=0; i<delims[n]; i++) {
					itemChars[i] = data.get(i);
				}
			}
			if ( n>0 && n<delims.length ) {
				itemChars = new char[(delims[n] - delims[n-1]) - 1]; // The size of the piece of data is (delims[n+1]-delims[n])-1
				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)
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			if ( n == delims.length ) {
				itemChars = new char[(data.length() - delims[n-1]) - 1];
				for (int i=delims[n-1]+1; i<data.length(); i++) {
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			return new MyString(itemChars);
		}
//  )
}

Revision: 34102
at October 17, 2010 12:00 by trusktr


Initial Code
public class DataReader {
	
	// Data Objects (
		private MyString data;
		private MyString delim; // The specified deliminator
		private int[] delims; // An array that will hold the positions of deliminators within a string of data.
//  )
	// Constructors (
		public DataReader() { // Default Constructor
			this(new MyString(), new MyString(" "), new int[]{0});
		}
		public DataReader(MyString s, MyString d, int[] dlms) { // Parameterized Constructor Template
			data = new MyString(s);
			delim = new MyString(d);
			delims = dlms;
		}
		public DataReader(MyString s) { // Parameterized Constructor
			this(s, new MyString(" "), new int[]{0});
		}
		public DataReader(String s, String d) { // Parameterized Constructor
			this(new MyString(s), new MyString(d), new int[]{0});
		}
		public DataReader(String s) { // Parameterized Constructor
			this(new MyString(s), new MyString(" "), new int[]{0});
		}
		public DataReader(DataReader d) { // Copy Constructor
			this(d.getData(), d.getDelim(), d.getDelims());
		}
//  )
	// Accessors (
		private MyString getData() {
			return new MyString(data);
		}
		private MyString getDelim() {
			return new MyString(delim);
		}
		private int[] getDelims() {
			return delims;
		}
//  )
	// Mutators (
		public void setBuffer(MyString d) {
			delim = new MyString(d);
			try {
				delims = data.findAll(delim.toString()); // The findAll method of my MyString class ended up coming very much in handy!!
			}
			catch (ArrayIndexOutOfBoundsException i) {
				delims = new int[]{0};
				System.out.println("Error: That deliminator was not found in the data.");
			}
		}
		public void setBuffer(String d) {
			this.setBuffer(new MyString(d));
		}
		public void setBuffer() {
			this.setBuffer(new MyString(" "));
		}
		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.
			char[] itemChars = null;
			if ( n==0 ) {
				itemChars = new char[delims[n]];
				for (int i=0; i<delims[n]; i++) {
					itemChars[i] = data.get(i);
				}
			}
			if ( n>0 && n<delims.length ) {
				itemChars = new char[(delims[n] - delims[n-1]) - 1]; // The size of the piece of data is (delims[n+1]-delims[n])-1
				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)
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			if ( n == delims.length ) {
				itemChars = new char[(data.length() - delims[n-1]) - 1];
				for (int i=delims[n-1]+1; i<data.length(); i++) {
					itemChars[i-(delims[n-1]+1)] = data.get(i);
				}
			}
			return new MyString(itemChars);
		}
//  )
}

Initial URL

                                

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

Initial Title
cisp401 DataExtractor.java

Initial Tags
data, java

Initial Language
Java