Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17



Posted on 2007-11-20
Medium Priority
Last Modified: 2010-03-30
I have written a code which reads each xml tag in each iteration. At every iteration I insert a forward slash '/' at the index of 1 for the first xml tag and compare it with the present tag entered. If both are sameit indicates the closing tag has been entered. It comes out of the loop. Intrestingly everytime I enter a forward slash, two slashes are getting inserted. I am not knowing the reason why. My code is given below.

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package scjp_chapter1;

 * @author prasad
//public class stringTest {



public class stringTest
    public static void main(String[] args)
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        InputStreamReader reader = new InputStreamReader( );
        BufferedReader br = new BufferedReader(reader);
        String str=null;
        System.out.println("Enter the Product Tree String. If no value is entered press enter");
        for(int index=0;;index++)
           //returnObj.setProductTree(readString("Product Tree", null));
               System.out.println("Enter String number "+ index);
                  if(str==null) break;
                  sb2.append (str);
                  System.out.println("sb1 is "+(sb1.insert(1,"/")).toString());
                  System.out.println("sb3 is "+sb3);
           }//End try block
           catch(IOException ioe)
          if((sb1.insert(1,'/')).toString().equals( sb3.toString ()))

Question by:rmvprasad
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 20325862
Its because you are inserting it twice

1) System.out.println("sb1 is "+(sb1.insert(1,"/")).toString());
2) if((sb1.insert(1,'/')).toString().equals( sb3.toString ()))

so two '/' are getting inserted

Accepted Solution

gkishoreji earned 375 total points
ID: 20325864
just comment out that print statement. it should work fine
LVL 35

Expert Comment

ID: 20326411
gkishoreji is right

StringBuffer is mutable.  I guess you had your "Strings are immutable" head on ;-)


PS:  All points should go to gkishoreji, with no points for this, I'm just rambling...

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question