Solved

Strings

Posted on 2007-11-20
3
220 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 125 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

626 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