Check if the input is palindrome - Java project

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 16181 Views - Last Post: 27 November 2009 - 04:24 PM Rate Topic: -----

#1 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Check if the input is palindrome - Java project

Post icon  Posted 27 November 2009 - 06:42 AM

Hello everyone




i need to write a program in java that will test if the input is a Palindrome or not and i have to write it using stacks and queues and LinkedList with an iterator
So here's what i've come up with till now:

  
	  /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);  
	  }
   
	  }
  
	   


i'm told that my program goes into an infinite loop and that i shouldn't test the input to see if the characters are letters. javadoc comments say things that are not accurate.

and here's the program for Linked lists and iterator
  
	  /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);
	  }	   
   }



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

Any help would be greatly appreciated, thanks

This post has been edited by Gaszam: 27 November 2009 - 10:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Check if the input is palindrome - Java project

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 08:39 AM

First, get rid of all the line numbers, they aren't necessary.

Second, allow me to be flabbergasted. To solve this problem which, at most requires a for loop, you have to use a stack, a queue, and a linked list?!?!
Was This Post Helpful? 0
  • +
  • -

#3 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 09:18 AM

Thanks about the advice for the lines, will do so. As for the second question ye the project requires me too use a stack and a queue with linked lists. To be more specific the project must contain 3 classes: Stacklist Queuelist and Palindrome. The last one will check if the input is palindrome or not
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,393
  • Joined: 27-December 08

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 09:28 AM

If I may make a suggestion for your algorithm, I would check to see if the given character is a number or digit using the Character.isLetterOrDigit(char) before adding it to the Stack and Queue. In this way, you eliminate punctuation, symbols and whitespace, which would otherwise throw your method off. Here's what I mean (ignore whitespace):

The String: A man, a plan, a canal, panama.
The Reverse: .a man a p,lan a ca,nal pa,nama

If you need more help, either please edit your code above to remove the numbers and include proper spacing and indentation or repost it to include the above changes to the formatting. Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#5 jtphenom  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 129
  • Joined: 06-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 09:36 AM

A few problems I'm noticing:

while (input.length() != 0);


There is no do to go with this while, and there needs to be if your while has a semi-colon at the end and no body afterwards.

	   
  40.
	  while (!queue.isEmpty())
  41.
	  {
  42.
	  if (queue.remove() != stack.pop())
  43.
	  count++;
  44.
	  }


Here you could just leave off the count, and as soon as it finds an unmatching character, it can return false. If it gets to the end of the queue without returning false, it can return true. So just return true after the end of the loop.

Quote

i'm told that my program goes into an infinite loop and that i shouldn't test the input to see if the characters are letters. javadoc comments say things that are not accurate.

Told by who? Yeah javadocs are like that sometimes, as is most documentation. It really is unnecessary to test for letters for this type of program, though, in my opinion. Either way, I don't see why doing that kind of test would give you an infinite loop, especially because the test is in an if statement, not a for statement.

Quote

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

What's it doing? Are you getting error messages? If so, what are they? Have you run it through a debugger?

I think that there should be a tutorial on here on how to use a basic debugger (like the one in jGrasp or something), if there's not already... ?
Was This Post Helpful? 0
  • +
  • -

#6 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 10:06 AM

Thanks for the suggestion macosxnerd101. The copy-paste thing wasnt that good indeed so i edited my code a bit in order for it to be more accurate and easy to read. Hope it's better now.
Was This Post Helpful? 0
  • +
  • -

#7 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 10:49 AM

In the first code i am gettin some errors:
/import java.util.*; Class,interface or enum expected

public class Palindrome class palindrome is public should be declared in a file named palindrome.java

And afterwards it cannot find symbols Class scanner, Class stack, class arrayqueue, class queue
Was This Post Helpful? 0
  • +
  • -

#8 jtphenom  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 129
  • Joined: 06-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 11:00 AM

View PostGaszam, on 27 Nov, 2009 - 09:49 AM, said:

In the first code i am gettin some errors:
/import java.util.*; Class,interface or enum expected

public class Palindrome class palindrome is public should be declared in a file named palindrome.java

And afterwards it cannot find symbols Class scanner, Class stack, class arrayqueue, class queue

Get rid of the slash before the word import. Also, what is the name of the java file you are writing this Palindrome class in?
Was This Post Helpful? 0
  • +
  • -

#9 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 11:23 AM

The 1st one worked out thanks. it is named NovemberProject
Was This Post Helpful? 0
  • +
  • -

#10 jtphenom  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 129
  • Joined: 06-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 11:24 AM

View PostGaszam, on 27 Nov, 2009 - 10:23 AM, said:

The 1st one worked out thanks. it is named NovemberProject

You need to name your files after your class. If your class is Palindrome, your file needs to be named Palindrome.
Was This Post Helpful? 1
  • +
  • -

#11 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 11:33 AM

Fixed too, Thanks :)
Was This Post Helpful? 0
  • +
  • -

#12 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 12:08 PM

The palindrome.class seems to work as intended. it prompts me to enter an input and its says if its is palindrome or not. however i can't figure where the queue-stack with linked lists are used when i run the program.Any ideas?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,393
  • Joined: 27-December 08

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 01:15 PM

I think what your instructor wants you to do is use a LinkedList, Queue, Stack and Iterator all in the same program, not write 2 separate programs. Now you can knock out the Queue and LinkedList all in one statement like so:
Queue<Character> queue = new LinkedList<Character>();



Since Queue is an Interface and LinkedList is the implementing class, this is a legal declaration. Basically, you are forcing a Queue behavior on a LinkedList.

Now let me ask you a question: if you wrote the code, why don't you know where these elements are used? This leads me to believe that you didn't actually write the code. If so, please re-read rule #1 before posting again. It states:

Quote

We will not do your homework for you!


And really, you aren't using a LinkedList. You are instead using an ArrayList and ArrayDequeue.

One more thing. The following is really bad practice:
List<Character> LinkedList = new ArrayList<Character>();


It implies to anyone reading your code that your variable has 2 types- List and LinkedList- but no name. While this is syntactically valid, it should be avoided like the plague. Instead, name it something meaningful like stringAsChars or inputChars. Also, a LinkedList is very different than an ArrayList, so the name is even more misleading.

This post has been edited by macosxnerd101: 27 November 2009 - 01:17 PM

Was This Post Helpful? 1
  • +
  • -

#14 Gaszam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-November 09

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 02:24 PM

First of all thanks for the time spent in order to help me.I understand your concern about the whole thing but i never asked u guys to do my homework, i presented a code and asked for some advices because i am not really that good in java programming like most of u guys are.Anyway i have been searching the web for 2-3 days now and sometimes i had to copy paste some parts related to queues/stacks general usage or even edit my code again and again in order to make some sense.The problem is that the code himself seems to make some sense but when it comes to runnin the whole program i just cant see where the linked lists occur. I will try to re-do the code tomorrow according to your suggestions and finalize the project because i am runnin out of time
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,393
  • Joined: 27-December 08

Re: Check if the input is palindrome - Java project

Posted 27 November 2009 - 02:38 PM

When the program is running, you won't physically see where the LinkedList (or any collection for that matter) is being used unless you have various components being outputted. For example, when you log in to DIC, do you really see/care if Chris is using a MySQL engine vs. a PostgreSQL engine? Or when you run a desktop app, do you know/care if they are using an ArrayList vs. LinkedList? I'm guessing probably not.

Now in terms of the homework, I just wanted to clarify a few things. We generally get a lot of homework requests (direct and not so direct) for things like give me the code, tell me what is happening here, what is this component, etc. And in reality, most of these requests come from members w/less than 10 posts. So hopefully no offense was taken at my reiteration of the rules, as I certainly didn't mean to offend anyone. And after your above post clarifying everything, I'm convinced that you're not here for a "what is happening here, what is this component" type homework question.

Thanks for your understanding. Keep up the good work! :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2