Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree2Likes

Thread: Palindrome

  1. #1
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Palindrome

    In my quest for a better understanding of Java I came across this program that looks like everyone is trying on for size. I am looking to see what i am doing wrong since it won't compile in Eclipse: here is the code if anyone can take pity on a newb...

    Java Code:
    package Palindrome;
    
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class Palindrome 
    {
    	static Scanner scanner = new Scanner(System.in);
    
    	public static int main(String[] args) 
    	{
    		int num = 0;
    
    		while (num >= 0)
    		{
    			JOptionPane.showMessageDialog(null,"Please enter a 5 digit number.Enter -1 to terminate the program:");
    			num = getInput();
    
    			while((num <= 9999)||(num > 99999))
    			{
    				JOptionPane.showMessageDialog(null,"You must enter a 5 digit number:");
    				num = getInput();
    
    				if (num == -1)
    				{
    					break;
    				}
    			}
    			if(isPalindrome(num))
    			{
    				JOptionPane.showMessageDialog(null, num + "is a palindrome");
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, num + "is not a palindrome");
    			}
    
    			public static boolean isPalindrome(int num)
    			{
    				int d1 = 0;
    				int d2 = 0;
    				int d3 = 0;
    				int d4 = 0;
    				int d5 = 0; 
    
    				d1 = num / 10000;
    				num = num % 10000;
    				d2 = num / 1000;
    				num = num % 1000;
    				d3 = num / 100;
    				num = num % 100;
    				d4 = num / 10;
    				num = num % 10;
    				d5 = num;
    
    				if ((d1 = d5)&&(d2 == d4))
    				{
    					return true();
    				}
    
    				else 
    				{ 
    					return false();
    				}
    			}
    
    			public static int getInput()
    			{
    				int num = 0;
    				String inputLine = "";
    				boolean goodData = false;
    
    				while (!goodData)
    				{
    					JOptionPane.showInputDialog("Enter a 5 digit number");
    					inputLine = scanner.nextLine();
    
    					try
    					{
    						num = Integer.parseInt(inputLine);
    						goodData = true;
    					}
    					catch(Exception e)
    					{
    						JOptionPane.showMessageDialog(null,"Your input must be numeric");
    						goodData = false;
    					}
    				}
    				return num;	
    			}
    		}
    
    	}
    }
    Last edited by addanny; 07-24-2014 at 07:06 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Palindrome

    The braces ( {} ) are not correctly placed.

    Java Code:
    else
    {
    JOptionPane.showMessageDialog(null, num + "is not a palindrome");
    }
    
    // there is one missing here
    
    public static boolean isPalindrome(int num)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Palindrome

    When posting code, please use the code tags to preserve formatting. You can go back and edit your post to include them.

    What is the full text of the compiler error? What exact line is it on? What happened when you googled the error?

    Edit- Too slow, Gimbal is better at reading unformatted code than I am. :p
    Last edited by KevinWorkman; 07-24-2014 at 07:03 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    Ok I got that fixed now it keeps telling me that the main function has to return a type int value. which I return on last line as return num; on line 99 why does it do that?

  5. #5
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    Sorry about code brackets, all fixed now. After fixing the brackets now on line 19 the message is "This method must return a type int" and gives me options for changing to void type. Why would it do that?? and can i change it to void without it having issues?

  6. #6
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    Also now on line 56 the if statement says that the && operator is undefined for the type INT boolean" but all the d 1thorugh d5 are declared and initialized???

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Palindrome

    One thing at a time. Where did you get this code? If you wrote it, you should have compiled much more often instead of waiting until you wrote the whole thing.

    Take your compiler errors one at a time. Try googling it, and if you can't figure it out, copy the full, exact text of a single compiler error here, along with the exact line that it's on.

    Edit- Look at the indentations of your code. This tells you what's "inside" each block of code. It looks like your main() method never ends, and you're trying to put other functions inside of main. Like gimbal already pointed out, this is because you're missing a closing curly brace. Hint: where does your main() method end?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    I got bits and pieces off of the internet. The way it was written originally it was a console based program. I like using the JOption for javax and I decided to change it a bit. Also what I got originally had a very convoluted logic behind it where the number from input was read forwards and then in revers. I want to prove to myself that within the logic when reading a number in a mathematical logical order by using integer division the same thing can be accomplished in less code lines. The only error I am having as of right now is this

    Java Code:
    if ((d1 = d5)&&(d2 == d4))
    		{
    			return true();
    		}
    
    		else 
    		{ 
    			return false();
    		}

    Where it tells me that the operator && is not defined for the types int,boolean

    also the words true and false are invalid according to the error message.


    I fized the d1 = d5 issue with d1== d5 as a boolean operator but the words false and true are still invalid
    Last edited by addanny; 07-24-2014 at 07:23 PM. Reason: Correction

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Palindrome

    You can't just download stuff off the internet and hope for the best. You have to understand what the code is doing. The approach you're taking isn't a good way to learn.

    There are no such things as true() or false() functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    You just answered my question. I did take the brackets away from true and false (was not sure if that's what it meant that the two would be methods). Now I have no errors I just need it to read the whole program. It stops after the input for the number and does not compile.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Palindrome

    Like I said, you can't just paste together a bunch of code you found on the internet. It's not going to work. It's impossible for us to help you without more specific information, which you can't give us because you don't understand the code. That's not meant as an insult.

    I highly recommend starting from scratch, writing code yourself, and compiling often.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    I did not take it as an insult. I came here to ask a question and I have answers. I will rebuild the code. You must admit that I understand what methods and logic are. The program as it sits now has no errors on it but when i run it it asks for the first number and then nothing. I think it is tuck in some sort of a loop. after the first show message dialog. I am not sure if the entered number is being stored properly. I will rewrite the code if I have to but i do understand it since it looks nothing like what i started off with. Thanks to you guys all the errors are gone. And please don't think you are insulting me by trying to teach me something. LOL

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Palindrome

    Try adding print statements around that loop. Also it seems strange that you're showing an input dialog but then trying to get a value from a Scanner, which is going to read from the command prompt. You might want to read up on the tutorial for input dialogs: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Palindrome

    a hint:
    where your true() and false() methods are declared?
    do you know that you must not use java reserved words in your naming?
    or if your intention is to use reserved words (as it is) then use it properly.

  15. #15
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    Guys I got it:::

    Thank you for your input:
    I rewrote it and here is what i came up with it works beautifully:

    Java Code:
    package Palindrome;
    
    import javax.swing.JOptionPane;
    
    public class Palindrome 
    {
    	public static void main(String[] args) 
    	{
    		int num = 0;
    
    		while (num >= 0)
    		{
    			JOptionPane.showMessageDialog(null, "Please enter a 5 digit number:");
    			num = getInput();
    
    			while((num <= 9999)||(num > 99999))
    			{
    				JOptionPane.showMessageDialog(null,"You must enter a 5 digit number:");
    				num = getInput();
    
    				if (num == -1)
    				{
    					break;
    				}
    			}
    			if(isPalindrome(num))
    			{
    				JOptionPane.showMessageDialog(null, num + " is a palindrome");
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, num + " is not a palindrome");
    			}
    
    			JOptionPane.showMessageDialog(null,"You must enter a 5 digit number:");
    			num = getInput();
    		}
    		return;
    	}
    	@SuppressWarnings("unused")
    	public static boolean isPalindrome(int num)
    	{
    		int d1 = 0;
    		int d2 = 0;
    		int d3 = 0;
    		int d4 = 0;
    		int d5 = 0; 
    
    		d1 = num / 10000;
    		num = num % 10000;
    		d2 = num / 1000;
    		num = num % 1000;
    		d3 = num / 100;
    		num = num % 100;
    		d4 = num / 10;
    		num = num % 10;
    		d5 = num;
    
    		if ((d1 == d5)&&(d2 == d4))
    		{
    			return true;
    		}
    
    		else 
    		{ 
    			return false;
    		}
    	}
    
    	public static int getInput()
    	{
    		int num = 0;
    		String numStr = "";
    		boolean valid = false;
    		String prompt = "";
    
    		while (!valid) 
    		{
    			try 
    			{
    				prompt = "Enter a 5 digit number "; 
    				numStr = JOptionPane.showInputDialog(prompt);
    				num = Integer.parseInt(numStr);
    				valid = true;
    			} 
    			catch (NumberFormatException e) 
    			{
    				JOptionPane.showMessageDialog(null, "Illegal input " + prompt, "Illegal ", JOptionPane.ERROR_MESSAGE);
    			}
    		}
    		return num;
    	}
    }

    I was using a scanner when I was also trying to use JOptionpane. Thank you for the quick responses and the web link. I Changed it all around and it now works. Now we know that this can be done with mathematical functions and not just reverse checks.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Palindrome

    Quote Originally Posted by KevinWorkman View Post

    Edit- Too slow, Gimbal is better at reading unformatted code than I am. :p
    No I'm not, I copy/pasted it into a Java source file and tried to compile it ;) I have a sandbox directory setup for command prompt compilation just for occasions like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    That ok because when i ran the code again I still have a couple of small bugs. I have to figure out the if statements. i think I am missing an else somehwere in there

  18. #18
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Palindrome

    if I may add..
    I didn't go through your other methods but my attention is drawn by your main method.
    its little messy. From my perspective, you have too many while loops.
    Much simpler code with if statements (of fewer while) will do the job here.
    just count in how many places you call getInput() method, or calling for option dialog to show the question. does is rings for "something is too much here"?

    try to put it on the paper:
    I need to fetch user's number - i will do it with optionpane.
    now i got the number - i must check if it fits my conditions and if it is palindrome
    so after checking I must output my answer to the user..
    depending on the my condition i will continue or exit


    of course it sounds simple but this is how you go to solve the problem and programming is just a tool to bring it alive..
    Last edited by milovan; 07-24-2014 at 10:25 PM.
    gimbal2 and JosAH like this.

  19. #19
    addanny is offline Member
    Join Date
    Jul 2014
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome

    You are amazing. That is all I needed. The program is completely. And after MANY tests is actually works as supposed to here is the FINAL code:
    package Palindrome;

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Palindrome2
    {
    	public static void main(String[] args) 
    	{
    		int num = 0;
    
    		JOptionPane.showMessageDialog(null, "Please enter a 5 digit number. Enter -1 to exit");
    		num = getInput();
    
    		while((num > 9999)||(num <= 99999))
    		{
    			if (num == -1)
    			{
    				break;
    			}
    			else
    			{
    				if(isPalindrome(num))
    				{
    					JOptionPane.showMessageDialog(null, num + " is a palindrome");
    					num = getInput();
    				}
    				
    				else
    				{
    					JOptionPane.showMessageDialog(null, num + " is not a palindrome");
    					num = getInput();
    				}				
    			}
    		}
    	}
    	@SuppressWarnings("unused")
    	public static boolean isPalindrome(int num)
    	{
    		int d1 = 0;
    		int d2 = 0;
    		int d3 = 0;
    		int d4 = 0;
    		int d5 = 0; 
    
    		d1 = num / 10000;
    		num = num % 10000;
    		d2 = num / 1000;
    		num = num % 1000;
    		d3 = num / 100;
    		num = num % 100;
    		d4 = num / 10;
    		num = num % 10;
    		d5 = num;
    
    		if ((d1 == d5)&&(d2 == d4))
    		{
    			return true;
    		}
    
    		else 
    		{ 
    			return false;
    		}
    	}
    
    	public static int getInput()
    	{
    		int num = 0;
    		String numStr = "";
    		boolean valid = false;
    		String prompt = "";
    
    		while (!valid) 
    		{
    			try 
    			{
    				prompt = "Enter a 5 digit number "; 
    				numStr = JOptionPane.showInputDialog(prompt);
    				num = Integer.parseInt(numStr);
    				valid = true;
    			} 
    			catch (NumberFormatException e) 
    			{
    				JOptionPane.showMessageDialog(null, "Illegal input " + prompt, "Illegal ", JOptionPane.ERROR_MESSAGE);
    			}
    		}
    		return num;
    	}
    }

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Palindrome

    I must say, its a very good effort. Its not "great" yet as you have yet to learn how to work with objects and so you make everything static, but that's a worry for later.

    One tiny, tiny, tiny little thing: try to avoid duplicate code.

    Java Code:
                    if(isPalindrome(num))
                    {
                        JOptionPane.showMessageDialog(null, num + " is a palindrome");
                        num = getInput();
                    }
                     
                    else
                    {
                        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
                        num = getInput();
                    }
    This works the same:

    Java Code:
                    if(isPalindrome(num))
                    {
                        JOptionPane.showMessageDialog(null, num + " is a palindrome");
                    }
                     
                    else
                    {
                        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
                    }
    
                    num = getInput(); // whatever the outcome of the above if/else, you want to read a next number.
    In that same snippet, you have the goal to display ONE message to the user: if the number is a palindrome or not. Do you really need that if/else block to show that ONE message?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Palindrome
    By threlot in forum New To Java
    Replies: 2
    Last Post: 11-22-2013, 07:52 AM
  2. palindrome
    By rochellelising in forum New To Java
    Replies: 1
    Last Post: 08-24-2011, 02:03 PM
  3. Palindrome
    By leepikamukharji in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 04:20 PM
  4. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 04:59 PM
  5. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •