Results 1 to 4 of 4
Thread: String problem: deleting
- 03-17-2011, 12:28 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
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.
- 03-17-2011, 01:13 PM #2
Is this input "try (a) checking (b)" is fixed or it can be changed like
try (c) checking (f) and so onsanjeev,संजीव
- 03-17-2011, 01:26 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
In that underlined bolded bit your comparison is looking ahead to the next character.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); }
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.
- 03-18-2011, 05:21 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Executing Command line commands from java - problem with deleting a file
By efebatistaarda in forum Advanced JavaReplies: 7Last Post: 02-10-2011, 07:37 PM -
Vector/Table Deleting Row Problem.
By ocean in forum New To JavaReplies: 8Last Post: 12-09-2009, 06:39 PM -
Hejp with deleting string from JTextField
By satin in forum New To JavaReplies: 9Last Post: 11-08-2008, 11:05 PM -
deleting characters from a String
By Hayzam in forum New To JavaReplies: 4Last Post: 08-29-2008, 12:14 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks