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

x
?
Solved

Strings

Posted on 2007-11-20
3
Medium Priority
?
222 Views
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 {

//}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

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( System.in );
        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));
           try
           {
               System.out.println("Enter String number "+ index);
               if(index==0)
               {
                  str=br.readLine();
                  if(str==null) break;
                  else
                  {
                     sb1.append(str);
                     sb2.append(str);
                  }
               }
               else
               {
                  str=br.readLine();
                  sb2.append (str);
                  sb3.delete(0,sb3.length());
                  sb3.append(str);
                  System.out.println("sb1 is "+(sb1.insert(1,"/")).toString());
                  System.out.println("sb3 is "+sb3);
               }
           }//End try block
           catch(IOException ioe)
           {
             ioe.printStackTrace();
           }
          if((sb1.insert(1,'/')).toString().equals( sb3.toString ()))
          {
                 //returnObj.setProductTree(sb2.toString());
                 break;
          }
        }

    }
}
0
Comment
Question by:rmvprasad
[X]
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
3 Comments
 
LVL 5

Expert Comment

by:gkishoreji
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
0
 
LVL 5

Accepted Solution

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

Expert Comment

by:TimYates
ID: 20326411
gkishoreji is right

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

Tim

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

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