[CLOSED] Extracting the " (Double Quote) character from a string
I have a string in the following pattern.
"xxx","xx",12.12.2011,16,"remarks"
I am parsing this string and writing to a database. I have written the code. But however I try, I am not able to succeed with double quote extraction. I want eliminate the Quotes when writing to database. My code is given below.
Code :
int asciiquote=34; // Ascii value for "
int asciichar=0;
String dateregex = "(0?[1-9]|[12][0-9]|3[01]).(0?[1-9]|1[012]).((19|20)\\d\\d)";
Pattern pat;
Matcher mat;
pat=Pattern.compile(dateregex);
String fldvals[]=new String[7];
InputStream fstream = fil.getInputStream();
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)
{
// Validate String
System.out.println(strLine);
++maxline;
i=0;
errors=false;
errmsg="";
StringTokenizer st = new StringTokenizer(strLine,",");
while(st.hasMoreTokens())
{
fldvals[i]=st.nextToken();
i++;
}
//now convert the values to uppercase after removing the " if any
for (i=0; i<fldvals.length; i++)
{
asciichar = Integer.parseInt(fldvals[i].substring(0,1));
if (asciichar==asciiquote)
{
fldvals[i]=fldvals[i].substring(1,(fldvals[i].length()-1));
fldvals[i]=fldvals[i].toUpperCase();
fldvals[i]=fldvals[i].trim();
}
else if (pat.matches(dateregex, fldvals[i]))
{
fldvals[i]=fldvals[i].replace(".", "/"); // replace "." in date with "/"
}
}
This is the final code trying the regular expressions.
I'm getting "java.lang.NumberFormatException: For input string: """
What's going wrong?
Re: Extracting the " (Double Quote) character from a string
You don't mention where the exception occurs, but I presume its where you use Integer.parseInt which, if you have quotes, will throw the exception. If all you want is to remove questes, just use String.replaceAll("\"", "");
If you think its the regular expression, I suggest you pull that out and test it in a short SSCCE to validate it
Re: Extracting the " (Double Quote) character from a string
Ya, you are right. The Exception occurs at the Integer.parseInt. And I only want to remove quotes from the string, provided they exist in the string. I tried
String.replaceAll("\\"", ""); But Eclipse is showing the following errors
Multiple markers at this line
- String literal is not properly closed by a double-quote
- Syntax error, insert ";" to complete BlockStatements
- The method replaceAll(String, String) in the type String is not applicable for the arguments
(String)
- Syntax error, insert ")" to complete MethodInvocation
Please Help
Re: Extracting the " (Double Quote) character from a string
You don't need to double escape the quotes (sorry about that, I've edited my post above)