I have a reader which is defined as follows:
CSVReader csvReader =new CSVReader(new InputStreamReader(inputStream), ',', '\'');
StringBuilder stringBuilder = new StringBuilder();
String[] line;
if (SPECIAL_CHARS == null || SPECIAL_CHARS.length == 0) {
return inputStream;
}
String[] stringArrSpecialChars = new String(SPECIAL_CHARS).split("");
while ((line = csvReader.readNext()) != null) {
//do somthing
}
how ever it throws this error:
java.io.IOException: Unterminated quoted field at end of CSV line. Beginning of lost text: [D'Olra
when it encounter a CSV cell with: D'Olra
Any advise ?
Fixed by replacing the first line with:
char charThatWillNotAppearInText = 127;
CSVReader csvReader = new CSVReader(new InputStreamReader(inputStream), ',', charThatWillNotAppearInText);
This is a workaround that disable the 'quotechar' option under the assumption that char with 127 will never be part of the input CSV file.
Use OpenCSV as:
com.opencsv.CSVParser parser = new CSVParserBuilder().withIgnoreQuotations(true).withSeparator('|').build()
Related
I need your help friends
I wrote a code that will extract records from SQL TABLE and use dataset to write it to XML, but i encounter a date convertion challenge after writting the XML the date change from dd/mm/yyyy to yyyy-mm-ddT00:00:00+00, in the process of correcting this, i found this code www.c-sharpcorner.com
//XMLTextReader
//Reads the Xml data generated by DataSet
XmlTextReader XTReader = new XmlTextReader(ds1.GetXml(),
XmlNodeType.Element, null);
//XMLTextWriter
//To write data into xml file
XmlTextWriter XTWriter = new XmlTextWriter(directoryPath + Tfilename.Trim() + ".xml", Encoding.UTF8);
XTWriter.WriteStartDocument();
string fieldName = "";
while (XTReader.Read())
{
switch (XTReader.NodeType)
{
//Check for the Node Name
case XmlNodeType.Element:
XTWriter.WriteStartElement(XTReader.Name);
fieldName = XTReader.Name;
break;
//Check for the Node Value
case XmlNodeType.Text:
//If the NodeName is PubDate
string Fname = "";
foreach (string mList in mFieldname)
{
Fname = mList.ToString().ToLower();
if (fieldName.ToLower() == Fname)
break;
}
if (fieldName.ToLower() == Fname)
//if (fieldName.ToLower() == SearchDate.ToLower())
{
DateTime dt = DateTime.Parse(XTReader.Value.ToString());
//Here the DateTime can be changed to any format as
//MM-dd-yyyy or dd-MM-yyyy or .ToShortDateString() ...
XTWriter.WriteString(dt.ToString("dd/MM/yyyy"));
}
else
XTWriter.WriteString(XTReader.Value);
// break;
//}
break;
case XmlNodeType.EndElement:
XTWriter.WriteEndElement();
break;
} //switch
} // while
XTWriter.Close();
this code worked fine in converting the date to british-UK format, The issue now is, it is changing the XML file from vertical arrengment to horizontal making it hard to read. Bellow is the result:
<?xml version="1.0" encoding="utf-8"?><NewDataSet> <PatientDemographics_Record><Lastname>Hidden</Lastname> <Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>263</RNL_SerialNO><Gender>Male</Gender><DateofBirth>11/01/1976</DateofBirth><AGE>40</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02371</PatientID><DateEnrolled>28/05/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>01/07/2015</ART_Start_Date><ptn_pk>5988</ptn_pk></WardVillageTown_OfResidence><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>574</RNL_SerialNO><Gender>Male</Gender><DateofBirth>11/04/1979</DateofBirth><AGE>36</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence>AWKUNTO</WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02348</PatientID><DateEnrolled>28/04/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>24/11/2015</ART_Start_Date><ptn_pk>5916</ptn_pk></PatientDemographics_Record><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>784</RNL_SerialNO><Gender>Female</Gender><DateofBirth>01/03/1990</DateofBirth><AGE>26</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>ENUGU</State_OfResidence><State_OfOrigin>ENUGU</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>02287</PatientID><DateEnrolled>17/02/2015</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>20/10/2015</ART_Start_Date><ptn_pk>6470</ptn_pk></WardVillageTown_OfResidence><PatientDemographics_Record><Lastname>Hidden</Lastname><Firstname>Hidden</Firstname><ClinicalVisit6MonthsPriorToReview>Yes</ClinicalVisit6MonthsPriorToReview><MaritalStatus>MARRIED</MaritalStatus><HosiptalNo>Null</HosiptalNo><RNL_SerialNO>842</RNL_SerialNO><Gender>Male</Gender><DateofBirth>15/05/1985</DateofBirth><AGE>30</AGE><HospitalAdmissionDuringReview>Null</HospitalAdmissionDuringReview><Occupation>Employed</Occupation><Education>Junior Secondary</Education><WardVillageTown_OfResidence>ATANI AROCHUKWU</WardVillageTown_OfResidence><LGA_OfResidence>UDI</LGA_OfResidence><State_OfResidence>IMO</State_OfResidence><State_OfOrigin>IMO</State_OfOrigin><Tribe>IBO</Tribe><FacilityID>1416110036</FacilityID><PatientID>01549</PatientID><DateEnrolled>01/11/2011</DateEnrolled><RecordCompletionPosition>16</RecordCompletionPosition><UploaderId>Null</UploaderId><UploaderDT>01/01/1999</UploaderDT><webUploadFlag>NO</webUploadFlag><ReviewPeriod>6</ReviewPeriod><ART_Start_Date>21/07/2015</ART_Start_Date><ptn_pk>4690</ptn_pk></PatientDemographics_Record></PatientDemographics_Record></PatientDemographics_Record></NewDataSet>
Try to set formatting and indentation properties of the XmlTextWriter.
https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.formatting(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.indentation(v=vs.110).aspx
In your case, add these lines after the creation of XmlTextWriter:
XTWriter.Formatting = Formatting.Indented;
XTWriter.Indentation = 4;
So my code so far reads in lines from a file, stores them in two separate ArrayLists. Now I'm going to have to make each node in a doubly Linked List contain the line's string and the corresponding integer, but since I cant put two ArrayLists in the declaration for a Linked List, I tried making a Tree map where the key is the string and the value is the corresponding integer. Here's the relevant code
public class FileReaderProgram
{
public static void main(String[] args) throws IOException
{
ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
String stringComponent = " ";
int integerComponent = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the absolute path of the file");
String fileName = in.next(); //Gets the file from the user
File inFile = new File(fileName);
**Map<String,Integer> lineCombo = new TreeMap<String,Integer>();**
try
{
Scanner fileReader = new Scanner(inFile); //Constructs Scanner for reading the file
fileReader.useDelimiter("\\n");
while (fileReader.hasNextLine())
{
String line = fileReader.nextLine(); //Gets line from the file
Scanner lineScanner = new Scanner(line); //Constructs new scanner to analyize the line
lineScanner.useDelimiter(",");
stringComponent = lineScanner.next(); //Read first word
while (!lineScanner.hasNextInt())
{
stringComponent = stringComponent + " " + lineScanner.next(); //Checks if more than one word part of string
}
integerComponent = lineScanner.nextInt(); //Read in integer
words.add(stringComponent); //Array of Strings, element number corresponding to the line it came from
numbers.add(integerComponent); //Array of Ints, same thing as above
**lineCombo.put(stringComponent, integerComponent);**
}
}
So when I run the file:
example string,1
another example,42
data,200
final,150
it prints out {another example=42, data=200, example string=1, final=150}
Why is it putting the strings in the wrong order?
I am creating an API/web service which needs to return JSON format.
I also need to create the web service as a POST request
Sample code below, see more snippets of the source code in the end of this post.
Meta meta = new Meta();
meta.recipes = new List<Recipe>();
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(meta);
return strJSON;
Problem:
When I try the response in a few REST consoles (list of consoles tried) and in the ASP.NET client, I get this format with an extra "d" and extra \ before each ". See return output below:
{"d":"{\"count\":\"0\",\"status\":\"500\",\"recipes\":[]}"}
When I try to remove serialization then I get the following format:
<Meta xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema"; xmlns="tempuri.org/">; <count>1</count> <status>200</status> <recipes> <Recipe> <recipeID>1</recipeID> <recipeName>Apple Pie</recipeName> <imageURL>service/it.jpg</imageURL> <rating/> </Recipe> </recipes> </Meta>
But I want it in the following format:
{"count":"0","status":"500","recipes":[]}
[WebMethod(Description = "Return all Recipe...")]
[ScriptMethod( ResponseFormat = ResponseFormat.Json)]
public Meta RecipeList(string ingredientId, string cuisineId, string dishTypeId, string courseId)
This still returns XML even though I return meta object and don't add serialization
Questions:
I thought the correct JSON format should be WITHOUT this "d" and the . Is this true or is the correct JSON format of the output actually WITH the "d" and the \?
If it should be without, then where do you suggest the correction should be made, on the server side or in the client side?
How should I correct this on the server side?
How can this be corrected on the client side?
[WebMethod(Description = "Return all Recipe...")]
[ScriptMethod( ResponseFormat = ResponseFormat.Json)]
public string RecipeList(string ingredientId, string cuisineId, string dishTypeId, string courseId,
string occasionId, string considerationId, string recipeType, string readyTime, string favouritebyUserId, string bookmarkbyUserId)
{
DataSet ds = new DataSet();
int rTime = 0;
if (readyTime == "") rTime = 0;
else rTime = Convert.ToInt32(readyTime);
ds = RecipeBLL.SearchRecipe(ingredientId, cuisineId, dishTypeId, courseId, occasionId, considerationId, recipeType, rTime);
// Create a multidimensional jagged array
string[][] JaggedArray = new string[ds.Tables[0].Rows.Count][];
int i = 0;
Meta meta = new Meta();
int count = 0;
meta.recipes = new List<Recipe>();
foreach (DataRow rs in ds.Tables[0].Rows)
{
Recipe recipe = new Recipe {
recipeID = rs["RecipeId"].ToString(),
recipeName = rs["RecipeTitle"].ToString(),
imageURL = rs["Photo"].ToString(),
rating = rs["Rating"].ToString()
};
meta.recipes.Add(recipe);
//mlist.Add(recipe);
count++;
}
if (count != 0)
meta.status = "200";
else
meta.status = "500";
meta.count = count.ToString();
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON1 = js.Serialize(meta);
return strJSON1;
}
It sounds like the problem is that you're returning a string from your code somewhere - and then it's being encoded as JSON by something else. So the string you're returning is:
{"count":"0","status":"500","recipes":[]}
... but whatever you're returning from thinks you're trying to return a string, rather than an object with a count etc.
You haven't shown any of your code, but I suspect the answer will be to just remove one explicit serialization call.
I have a piece of code that opens a HTTP connection to read the data contained on a webpage.
HttpConnection h = new HttpConnection();
InputStream input = h.openInputStream();
int len = (int) h.httpConn.getLength();
StringBuffer raw = new StringBuffer();
if(len > 0)
{
byte[] data = new byte[len];
while( -1 != (len = input.read(data)))
{
raw.append(new String(data, 0, len));
}
}
response = raw.toString();
input.close();
h.httpConn.close();
//System.out.println("Response -----> " + response);
return response;
This code works absolutely fine, but on certain punctuation marks its not reading properly. For example >> ' << an apostrophe comes out as >> รข <<.
I'm guessing it might be something to do with encoding but I have tried UTF-8, UTF-16 and ASCII and this didn't fix the problem.
I've had this work for me when I was getting odd characters in place of punctuation:
public String getContents(InputStream stream) {
String contents = null;
try{
contents = new String(IOUtilities.streamToBytes(stream), "UTF-8");
}
catch(Exception e) {
//encoding error
}
return contents;
}
Instead of this:
raw.append(new String(data, 0, len));
you can use
raw.append(new String(data, 0, len, "UTF-8"));
passing in the name of a character enconding, in this case UTF-8.
I am beginner in JavaME. I'd like to make simple dicitionary. The source data is placed on "data.txt" file in "res" directory. The structure is like this:
#apple=kind of fruit;
#spinach=kind of vegetable;
The flow is so simple. User enters word that he want to search in a text field, e.g "apple", system take the user input, read the "data.txt", search the matched word in it, take corresponding word, and display it to another textfield/textbox.
I've managed to read whole "data.txt" using this code..
private String readDataText() {
InputStream is = getClass().getResourceAsStream("data.txt");
try {
StringBuffer sb = new StringBuffer();
int chr, i=0;
while ((chr = is.read()) != -1)
sb.append((char) chr);
return sb.toString();
}
catch (Exception e) {
}
return null;
}
but I still dont know how to split it, find the matched word with the user input and take corresponding word. Hope somebody willing to share his/her knowledge to help me..
Basically you want something like this:
private String readDataText() {
InputStream is = getClass().getResourceAsStream("data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
try {
while ((line = br.readLine()) != null)
{
String[] split = line.split("=");
if (split[0].equals("#someFruit"))
return split[1];
}
}
catch (Exception e) {}
return null;
}
Read the line using a BufferedReader, no need to handle single chars.
Split the line by the = token
Check if the key in the dictionary is the wanted key, if so, return the value.