Solved

Strings

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

760 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

19 Experts available now in Live!

Get 1:1 Help Now