Results 1 to 13 of 13
  1. #1
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default HELP...Palindrome

    hey, how's everyone,

    i need help with a java program, i'll appreciate any help.

    i need to write a program in java that will test if the input is a Palindrome or not.
    i found a couple of programs, but they use a (do-while) loop which i'm not allowed to use.

    i have to write it using stacks and queues on the first program and LinkedList with an iterator on the second program

    here's my first program:



    Java Code:
    import java.util.*;
    
    public class Palindrome
    {
    
       public static void main(String[ ] args)
       {
    	Scanner palindrome = new Scanner(System.in);
    	String input;
        
    		{
    		
    	 System.out.print("Enter Input: ");
             input = palindrome.next();
    	 if (isPalindrome(input))
    	 
    	    System.out.println("That is a palindrome.");
    	 else
    	    System.out.println("That is not a palindrome.");
    		}
          while (input.length() != 0);
       }
    
    
       public static boolean isPalindrome(String input) 
       
    {   
          Queue<Character> queue = new ArrayDeque<Character>(); Stack<Character> stack = new Stack<Character>();
    	  Character letter;
          int count = 0;
          int i;
          
          for (i = 0; i < input.length(); i++) 
          {
    	 letter = input.charAt(i);
             if (Character.isLetter(letter))
             {
                queue.add(letter);
                stack.push(letter);
             }
          }
          
          while (!queue.isEmpty())
          {
             if (queue.remove() != stack.pop())
                count++; 
          }
    
          return (count == 0); 
       }
        
    }


    the program produces the correct response, but never terminates because it goes into an infinite loop... i shouldn't test the input to see if the characters are letters. and i'm suppose to include javadoc comments.

    i'll appritiate any help... :)
    _____________________________________________
    Here is the 2nd program:

    Java Code:
    import java.util.*;
    
    public class Palindrome
    {
    
       public static void main(String[ ] args)
       {
    	Scanner palindrome = new Scanner(System.in);
    	String input;
    	List<Character> LinkedList = new ArrayList<Character>();
    		{
    	
    	 System.out.print("Enter Input: ");
             input = palindrome.next();
    	 if (isPalindrome(input))
    		    System.out.println("That is a palindrome.");
    	 else
    	    System.out.println("That is not a palindrome.");
    		}
    
       }
    
        public static boolean isPalindrome(String input)
       {
    	List<Character> LinkedList = new ArrayList<Character>();
    	int count = 0;
    	int i;
    	ListIterator begin = LinkedList.listIterator();
    	ListIterator end = LinkedList.listIterator();
          for (i = 0; i < input.length(); i++)
    		{
    			LinkedList.add(input.charAt(i));         
    		}
    
          {
             if (!(begin.next() == end.previous())) 
                count++; 
          }
    
               return (count == 0); 
       }
        
    }
    here's the problems:

    program will not run to completion, so it cannot produce correct output.

    please help

    thanx ,

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I tried the first program, and it seems to work for me. Your infinite loop is because your while statement. By the look of it, you meant it to be a do..while loop. However, there is no "do". So, instead, what you have is an empty while loop that is true (and is always true) - yielding an infinite loop.

    Java Code:
    public static void main(String[] args)
    {
    	Scanner palindrome = new Scanner(System.in);
    	String input;
    
    	// your code has no "do"
    	[COLOR="Red"]do[/COLOR] {
    
    		System.out.print("Enter Input: ");
    		input = palindrome.next();
    		if (isPalindrome(input))
    
    			System.out.println("That is a palindrome.");
    		else
    			System.out.println("That is not a palindrome.");
    	}
    
    	// without the "do", this becomes a while loop
    	// since input.length() != 0, it loops
    
    	// input.length is always positive - leading to an infinite loop
    	while (input.length() != 0);
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    thank you for your help ,but is it possible to do the program with out a "do-while" loop, just a while , or for loop?

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    In your main method? You can always convert a do..while to a while loop, but why would you want to? A do..while is if you know it will run once, which in your case, it will.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    i can't use a "do-while", stupied assignment rules...
    is it possible to write it using "for" loop

    thank you very much for your help

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    A for loop isn't possible, because your method should repeat until the input is the empty string, right?

    However, you can convert your do-while loop to a while loop. It's not as clean, but what assignment yields clean code?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    how is it possible to do a while loop, do you mean just keep my code "as is", and how would i stop the ininite loop?!

    thank you for your time

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CodesAway View Post
    I tried the first program, and it seems to work for me. Your infinite loop is because your while statement. By the look of it, you meant it to be a do..while loop. However, there is no "do". So, instead, what you have is an empty while loop that is true (and is always true) - yielding an infinite loop.

    Java Code:
    public static void main(String[] args)
    {
    	Scanner palindrome = new Scanner(System.in);
    	String input;
    
    	// your code has no "do"
    	[COLOR="Red"]do[/COLOR] {
    
    		System.out.print("Enter Input: ");
    		input = palindrome.next();
    		if (isPalindrome(input))
    
    			System.out.println("That is a palindrome.");
    		else
    			System.out.println("That is not a palindrome.");
    	}
    
    	// without the "do", this becomes a while loop
    	// since input.length() != 0, it loops
    
    	// input.length is always positive - leading to an infinite loop
    	while (input.length() != 0);
    }
    This code doesn't appear to ever end as you should use Scanner#nextLine() rather than Scanner#next(). Also, you should skip the if/else blocks if input length == 0. Finally, always enclose all blocks in curly braces:
    Java Code:
          do {
             System.out.print("Enter Input: ");
             input = palindrome.nextLine();
             if (input.length() > 0) {
                if (isPalindrome(input)) {
    
                   System.out.println("That is a palindrome.");
                }
                else {
                   System.out.println("That is not a palindrome.");
                }
             }
          }
          while (input.length() != 0);


    Quote Originally Posted by CodesAway
    A for loop isn't possible, because your method should repeat until the input is the empty string, right?
    It isn't?
    Java Code:
          boolean keepGoing = true;
          for (; keepGoing; ) {
             System.out.print("Enter Input: ");
             input = palindrome.nextLine();
             if (input.length() > 0) {
                if (isPalindrome(input)) {
    
                   System.out.println("That is a palindrome.");
                }
                else {
                   System.out.println("That is not a palindrome.");
                }
             }
             else {
                keepGoing = false;
             }
          }
    Though admittedly, this isn't what for loops were intended for and I wouldn't recommend this code to anyone, but I have to refute your "isn't possible" statement as it's just not true.

  9. #9
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    A do-while loop executes at least once. A while loop can execute zero times. So, in order to allow the while loop to behave like a do-while, make sure that the loop can execute at least once.

    Java Code:
    do {
    	System.out.print("Enter Input: ");
    	input = palindrome.next();
    	if (isPalindrome(input))
    
    		System.out.println("That is a palindrome.");
    	else
    		System.out.println("That is not a palindrome.");
    } while (input.length() != 0);

    In some cases (like this one), you can convert a do-while DIRECTLY into a while loop.

    A do-while loop is IDENTICAL to a while loop, except how it handles the first iteration. A do-while ALWAYS has a first iteration, while a while loop may not (if the condition is false).

    For example, if you were to convert your above do-while into a while loop, input would need to be initialized. Can you think of a value to initialize input to that would cause input.length() to not equal 0? - allowing the loop to execute at least once.
    Last edited by CodesAway; 11-13-2009 at 03:07 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    Though admittedly, this isn't what for loops were intended for and I wouldn't recommend this code to anyone, but I have to refute your "isn't possible" statement as it's just not true.
    Sorry, you're right.

    When I said "isn't possible", I meant more a practical "for loops are for iterating a set number of times" kind of possible. Not a "if the assignment asked you to convert a do-while to a for loop" kind of possible.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #11
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    but how :(

  12. #12
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by CodesAway View Post
    Can you think of a value to initialize input to that would cause input.length() to not equal 0? - allowing the loop to execute at least once.
    This is my hint.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  13. #13
    d7o0om is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

Similar Threads

  1. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 06:35 PM
  2. palindrome problem
    By Blaedel in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 12:52 AM
  3. Palindrome problem
    By Mika in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:46 PM
  4. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 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
  •