Solved

Strings

Posted on 2007-11-20
3
212 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now