The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Recursive Strings (or rather strangulation)

Posted by Ken Wunderlich on October 21, 2001 at 8:42 PM

I'm a first year student and this program is required. I have some issues with the boolean return logic but I'm stuck. The requirement is to capture a string (keyboard input which is fine) and test to see if:

a) The string begins with either an x or x with an indefinite # of y's.
b) followed by a qz or qd.
c) followed by either an x or xyyyyyy.....
Take a look:
-----------------------------------------------------------
boolean analysis (int left, int right , String str)//blurb
{ foundX = false;
right = str.length();
str = str.toLowerCase();
if(left <= str.length())
{
//if(str.startsWith("x"))
if(index < str.length()&& str.startsWith("x")) //replace above
{foundX = true;

recognizeY(str,left+1);
recognizeQ(str,left+1);

return foundQ;}
else{System.out.println("x: " + false);
return foundX;}

}
System.out.println("Something not found: " + true);
return foundX;
}

boolean recognizeY(String str, int index)//Whooszit
{

if(index < str.length())//removed =
if (str.charAt(index) == 'y')
{foundY= true; //str = str.substring(0);

recognizeY(str, index+1);
System.out.println("foundY1: " + foundY);
return foundY;}

//str = str.substring(0);//changed 1 to 0

System.out.println("foundY2: " + foundY);
return foundY;}

boolean recognizeQ(String str, int index)//Whatszit
{

System.out.println("str: "+str);
System.out.println("index: "+index);
// index++;
// foundQ = false;
if (str.charAt(index) == 'q'&&foundY == true)
//str = str.substring(1); //new change
{foundQ=false;//changed true to false
System.out.println("str: "+str+" index: "+ index+" foundQ: "+foundQ);
recognizeQ(str,index);
return foundQ;}

if(str.charAt(index) == 'd' || str.charAt(index) == 'z')
{str = str.substring(1);
return foundQ = true;}
else
{// return foundQ;
// else
// {foundQ = true;
System.out.println("foundQ2: "+foundQ);
return foundQ;}//added new
}


----------------------------------------------------------
> Ack! This forum needs a preview before post!
> Here are the unadorned urls, since I will probably make another typo in correcting the original:
>


> J2SE 1.4: http://java.sun.com/j2se/
> Jython: http://www.jython.org/
> Third Party: http://industry.java.sun.com/solutions/?frontpage-main
>

> - mfg
>



> > Sounds like you need regular expressions.
> >
  • J2SE 1.4 will have them in util.regex, but that is not yet released. You can download or find more details on the beta on Sun's website (above link).
  • If you don't want to use a beta you could use Jython, which is Python running on Java.
    > >
  • Finally, there are third party tools for Java that do regular expressions; go to Sun's Solution Marketplace and search for "regular expression."

    > > - mfg

    > > > How do you successfully identify patterns in a string using recursive methods. Let's say I want to identify a pattern of
    > > > x or xy...(+)qz or qd (+) x?

    > > > I appreciate any help you can provide.
    > > > K






    Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us