Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Checking Blurbs

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checking Blurbs

    Hi, finishing my first year of C.S., and now I'm stuck on my last homework problem. I need to check whether a user entered String is a Blurb. A Blurb is a Whoozit followed by one or more Whatzits. A Whoozit is the character x followed by zero or more ys. A Whatzit is a q followed by either a z or a d, followed by a Whoozit. It keeps returning false, even though it's true. I used the debugger to find out that it is returning false when it's checking whether the first whatzit is a "qd" or "qf." I'm typing in xqdx, and for some reason it's telling me that it isn't qd, I guess. The problem is in the isWhatzit() method.

    import java.util.*;
     
    public class CheckBlurb 
    {
    	static int count = 0;
     
    	public static void main(String[] args) 
    	{
    		String userInput;
    		Scanner scan = new Scanner(System.in);
     
    		System.out.println("Enter a blurb.");
    		userInput = scan.next();
     
    		boolean isValid;
     
    		int checkStart = isWhoozit(userInput);
     
    		if(checkStart == 0)
    		{
    			isValid = false;
    		}
    		else
    		{
    			String nextCheck = (userInput.substring(checkStart));
    			isValid = isWhatzit(nextCheck);
    		}
     
    		if(isValid == true)
    		{
    			System.out.println("Yep, that is a Blurb.");
    		}
    		else
    		{
    			System.out.println("Nope, not a Blurb, sorry.");
    		}
     
    	}
     
    	public static int isWhoozit(String str)
    	{
     
    		if(str.charAt(count) == 'x')
    		{
    			count ++;
    			while(str.charAt(count) == 'y')
    			{
    				count++;
    			}
     
    		}
    		else
    		{
    			count = 0;
    		}
    		return count;
     
    	}
     
    	public static boolean isWhatzit(String str)
    	{
    		if(str.substring(0,1) != "qd" || str.substring(0,1) != "qz")
    		{
    			return false;
    		}
     
    		str = str.substring(2);
     
    		int nextWhoozit = isWhoozit(str);
     
    		if (nextWhoozit == 0)
    		{
    			return false;
    		}
     
    		else if (nextWhoozit >= str.length())
    		{
    			return true;
    		}
     
    		else
    		{
    			return isWhatzit(str.substring(nextWhoozit));
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Checking Blurbs

    if(str.substring(0,1) != "qd" || str.substring(0,1) != "qz")
    You're incorrectly using the comparison operators instead of using the equals method. See: Common Java Mistakes: == operator or equals() method (note that this is valid for both the != and == operator).
    Last edited by helloworld922; May 7th, 2011 at 12:06 AM.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking Blurbs

    Right, I realized that and changed it. It worked, but backwards. Not really sure how to explain it, but I was able to make it work. Thanks for the reply.

Similar Threads

  1. Need help with checking passwords
    By kb1213 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 12th, 2011, 05:32 PM
  2. Variable Type-Checking
    By richip in forum Collections and Generics
    Replies: 7
    Last Post: March 17th, 2011, 06:25 PM
  3. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  4. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM
  5. Replies: 2
    Last Post: February 4th, 2009, 11:24 AM