Results 1 to 4 of 4
  1. #1
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default String problem: deleting

    I got this program in my practical paper. We were asked to input a string like "try (a) checking (b)" and print "TRY CHECKING"
    I managed to get my output as "TRY ) CHECKING". I'm copying a part of my code. Any suggestions on how to eliminate the remaining parentheses will be great!
    code:

    public void check()
    {
    len=n.length();

    for(int i=0;i<len;i++)
    {
    ch=n.charAt(i);
    if((ch!='(')||(ch==' '))
    {
    temp=temp+ch;
    store=temp.toUpperCase();
    }
    else if(ch=='(')
    {
    do
    {
    store1=store1+ch;
    i++;
    }
    while((n.charAt(i+1)!=')'));
    }
    }
    System.out.println("String:"+store);
    }

    n is my original string, temp, store and store1 are extras for storing the changes in n.

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Is this input "try (a) checking (b)" is fixed or it can be changed like

    try (c) checking (f) and so on
    sanjeev,संजीव

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Java Code:
    public void check()
        {
            len=n.length();
            
            for(int i=0;i<len;i++)
            {
                ch=n.charAt(i);
                if((ch!='(')||(ch==' '))
                {
                    temp=temp+ch;
                    store=temp.toUpperCase();
                }
                else if(ch=='(')
                {
                    do
                    {
                        store1=store1+ch;
                        i++;
                    }
                    [B][U]while((n.charAt(i+1)!=')'));[/U][/B]
                }
            }
            System.out.println("String:"+store);
        }
    In that underlined bolded bit your comparison is looking ahead to the next character.
    The loop exits when that character is the close bracket.
    Then you go back to the for loop, and i is incremented and you are now looking at the close bracket, which you then append to your result.

    You want to do..while until the acharacter at index i is not equal to the close bracket. Not i + 1.

  4. #4
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 02-10-2011, 07:37 PM
  2. Vector/Table Deleting Row Problem.
    By ocean in forum New To Java
    Replies: 8
    Last Post: 12-09-2009, 06:39 PM
  3. Hejp with deleting string from JTextField
    By satin in forum New To Java
    Replies: 9
    Last Post: 11-08-2008, 11:05 PM
  4. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 12:14 PM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •