Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strings

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

877 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