Could you please show me how to code this in the most easy and simple way possib
ID: 3912717 • Letter: C
Question
Could you please show me how to code this in the most easy and simple way possible?
CSVParser.Java
//FIXME Add your own file header comment here
/**
* This class contains methods to parse text, specifically intended to eventually parse CSV (comma separated values)
* files.
*
* Definitions of Terms:
* delimiter: The character used to specify the boundary between fields in a sequence of characters.
* for example, the comma would be the delimiter between the fields:
* name, id, section
* Note the field values include the spaces. In general, the field value is every character except
* for the delimiter.
*
* qualifier: If a field value contains a delimiter such as a comma with the name field:
* John Doe, MD, 1234567, 321
* then a qualifier, such as ", is placed around a field value to indicate it is the same field.
* for example:
* "John Doe, MD", 1234567, 321
* If the qualifier itself is a part of the field value then the qualifier is duplicated.
* for example for the field value:
* H. Jackson Brown, Jr. said "The best preparation for tomorrow is doing your best today."
* would be enclosed with " and internal quotes duplicated resulting in the qualified field value:
* "H. Jackson Brown, Jr. said ""The best preparation for tomorrow is doing your best today."""
*
* A example with , as delimiter and " as qualifier with a newline in a field value should be
* processed as follows:
* "This is a line of text.",bbb,ccc
* would have 3 fields
* "This is a line of text."
* bbb
* ccc
*
* This project is derived from RFC 4180: https://tools.ietf.org/html/rfc4180
*
*/
public class CSVParser {
/**
* This method returns the array of string containing the field values.
* The field values in text are identified using , as the delimiter.
* Qualifiers are not considered at all in the parsing of text.
*
* Example:
* aaa,bbb,ccc
* should result in an array with 3 fields
* aaa
* bbb
* ccc
*
Explanation / Answer
Although the question is not very clear to me itself as it says a lot of things but I just got the point that you need a code for splitting the string with a delimiter without using split function. I will try to code that.
just change the string and the delimiter and you can also do your changes as you want the output string to be shown i will just show you one version where you can split your string using any delimiter.
import java.util.ArrayList;
import java.util.List;
public class MyOwnSplitClass {
public static void main(String[] args) {
String inputString = "This % is so%me spliited % t%ext"; //change to any string
String[] output = splitMine(inputString, '%'); //change the delimiter and try
System.out.println("please find your splitted text --> ");
for (int i = 0; i < output.length; i++) {
System.out.println(output[i]);
}
}
public final static String[] splitMine(String str, char separatorChar) {
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return null;
}
List<String> list = new ArrayList<String>();
int temp =0;
int fromIndex = 0;
while (temp>=0){
temp = str.indexOf(separatorChar,fromIndex);
if(temp<=0){
list.add(str.substring(fromIndex,len));
break;
}else{
list.add(str.substring(fromIndex,temp));
}
fromIndex = temp+1;
}
String[] arr = new String[list.size()];
list.toArray(arr);
return arr;
}
}