Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Extracting the " (Double Quote) character from a string

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up [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.

    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?
    Last edited by jai; April 4th, 2011 at 07:42 AM. Reason: closed


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default 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
    Last edited by copeg; March 28th, 2011 at 08:57 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default 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)

Similar Threads

  1. [SOLVED] Found String Requires Double Problem.
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 12:24 PM
  2. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 09:24 PM
  3. "previously declared string"?... help
    By Taylorleemusic in forum Object Oriented Programming
    Replies: 3
    Last Post: September 15th, 2010, 10:57 PM
  4. Replies: 1
    Last Post: January 15th, 2010, 12:32 AM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM