Fill up a string with spaces using StringBuffer

Discussions

Web tier: servlets, JSP, Web frameworks: Fill up a string with spaces using StringBuffer

  1. Fill up a string with spaces using StringBuffer (4 messages)

    How to fill up a string with spaces, where the space is a variable? Is the StringBuffer appropriate to use, or should I use other classes? please suggest an example.

    //want to fill the faxno up to 13 chars long
    //if faxno is initialised as 123456, then the rest of chars
    //should be spaces
    StringBuffer faxno = new StringBuffer(13);
    faxno.append("123456");
    String space=" ";
    char[] str=space.toCharArray();
    faxno.append(str, faxno.length()+1, 13-faxno.length());
    out.println("faxno=" + faxno.toString() + "END");

    >>> this is compiled successfully, but it has runtime error ,as stated as below:
    *****************************************************
    Apache Tomcat/4.0.1 - HTTP Status 500 - Internal Server Error

    --------------------------------------------------------------------------------

    type Exception report

    message Internal Server Error

    description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

    exception

    java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.lang.StringBuffer.append(StringBuffer.java:498)
    at org.apache.jsp.teststring$jsp._jspService(teststring$jsp.java:71)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet$JspServletWrapper.service(IDEJspServlet.java:174)
    at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.serviceJspFile(IDEJspServlet.java:247)
    at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.service(IDEJspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:223)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
    at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
    at org.netbeans.modules.web.monitor.catalina.MonitorValve.invoke(MonitorValve.java:142)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
    at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
    at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
    at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)
    at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)
    at java.lang.Thread.run(Thread.java:536)



    --------------------------------------------------------------------------------

    *****************************************************

    Threaded Messages (4)

  2. I'm not sure I understand your goal. What do you want faxno to be?
  3. off by 1[ Go to top ]

    I think you've got your start index wrong. Idexed from 0. so if faxno.length() is 6, you want to start filling from index 6 because the 6th character is in index 5 (since the first one is in index 0). faxno.append(str, faxno.length(), 13-faxno.length());
  4. That last one was wrong[ Go to top ]

    Actually, that's not what append does. The length there is not how many times to put that character in, but is the number of characters from your space character to put into the stringbuffer, that's where the overflow is coming from. Try Array.fill() instead
  5. Here's a solution[ Go to top ]

    Here's something I did to expand to 64 characters. Should be trivial from here. private String expandTo64Characters(String string) { char[] asChars = Arrays.copyOf(string.toCharArray(), 64); Arrays.fill(asChars, string.length(), asChars.length - 1, 'Z'); return new String(asChars); }