troubleshooting Question

Handling user input - apostrophe problem(array out of index error)

Avatar of dkim18
dkim18 asked on
Java
9 Comments1 Solution306 ViewsLast Modified:
Hi!

I am figuring out this function (someone else’s code) that throws array out of index error. I think this function was written to add single quote mark in user’s input if there is apostrophe because oracle didn't handle it properly.  When I tried to debug, I found out that this happens when empty string is entered. Can somebody know why and is there better solutions?

++++++++++++++++
      public String addSingleQuoteToApostrophe(String s) {
                         boolean start = false;
                         boolean end = false;
                         StringBuffer sb = new StringBuffer(100);
                         char c = s.charAt(0);
                         if(String.valueOf(c).equals("'"))
                           start = true;
                         
                         char e = s.charAt(s.length()-1);
                         //System.out.println("E = "+String.valueOf(e));
                         if (String.valueOf(e).equals("'"))
                              end = true;
                  
                         StringTokenizer st = new StringTokenizer(s,"'");
                         int count = st.countTokens();
             
                         if(count >= 1) {
                          for(int i=0;i<count;i++)    
                               sb.append(st.nextElement()+"''");
                          sb.replace(sb.length()-2,sb.length(),"");
                         }
                         else {
                   
                              return s;
                         }
                         
                        if(start)
                              sb.insert(0,"''");
                        
                        if(end)
                              sb.insert(sb.length(),"''");
                
                                return sb.toString();
      }
+++++++++++++++++++

thanks,


Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros