Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Junky

Thread: Classic Palindrome problem

  1. #1
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Classic Palindrome problem

    Hi I am struggling hard in Java and I have to take it for my school etc.

    I am decent with coding (I think) but here is the problem that I am having. I don't know how to call my methods that I have created within my main method. I need help calling my retrieveinput method etc. Any help would be nice, thank you :)

    Java Code:
    import javax.swing.JOptionPane;
    
    public class PalindromeMethod {
    	int num;
        int digit1;
        int digit2;
        int digit4;
        int digit5;
        int digits;
        
        int a, b, c;
        boolean palin;
        
        
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		
    		String numstr =JOptionPane.showInputDialog("Enter a 5 digit number");
    		
    		
    		
    	
    	}
    	public int retrieveinput (String numstr)
    	{
    		num = Integer.parseInt(numstr);
    		
    		if (num > 9999 && num < 100000) {
    			
    		}
    		else System.out.println("Please enter 5 digit number");
    		
    		return num;
    	}
    		
    		public void checkPalindrome (int num){
    			digits = 0;
    			
    			digit1=num/10000;
    			a=num%10000;
    			digit2=a/1000;
    			b=a%1000;
    			c=b%100;
    			digit4=c/10;
    			digit5=c%10;
    			if (digit1 == digit5 && digit2 == digit4){
    				palin = true;
    			}
    			else palin = false;
    }
    		public void displayPalindrome (boolean palin){
    			if (palin = true){
    				System.out.println(num + " is a Palindrome.");
    			}
    			else System.out.println(num + " is not a Palindrome.");
    		}
    	}
    Last edited by nanderson05; 05-13-2012 at 12:57 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Classic Palindrome problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Classic Palindrome problem

    All non-static methods belong to an instance of the class. That means when you are trying to call retrieveInput() method or checkPalindrome() method, you have to create an instance of the class first. After you have the object you can call all the method in it. To create an instance of the class use the new keyword.

  4. #4
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Classic Palindrome problem

    So I have to make a new palindromeMethod object? Where do I insert that?

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Classic Palindrome problem

    Yes, you have to create a PalindromeMethod object and you can do it in the main() method of your class.

  6. #6
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Classic Palindrome problem

    Main meaning:

    Java Code:
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             
             
            String numstr =JOptionPane.showInputDialog("Enter a 5 digit number");
             
             
             
         
        }
    So would I do this?

    Java Code:
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             
             
            String numstr =JOptionPane.showInputDialog("Enter a 5 digit number");
             
             Palindrome PalindromeMethod = new PalindromeMethod();
    
             
         
        }
    Does this look correct?

    Classic Palindrome problem-java-palindrome1.jpg

    Still getting errors as you can see :(

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Classic Palindrome problem

    Quote Originally Posted by nanderson05 View Post
    Still getting errors as you can see :(
    Yes, and the error tells you exactly what's wrong.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Classic Palindrome problem

    You need to clean up your code a bit for starters. An example is your retrieveinput method, it should be:

    retrieveInput();

    Also, numstr should be written like:

    numStr

    Be sure to capitalize those variables, you'll lose easy points that way.

    I am also inclined to ask, where is the constructor for the class?

    String numstr =JOptionPane.showInputDialog("Enter a 5 digit number");

    Looks like you are trying to invoke a non-existent constructor which takes in a String. You probably have another class somewhere else?

  9. #9
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Classic Palindrome problem

    No I don't have another class somewhere else, and I know that the error message is telling me whats wrong but I don't understand what it means because I am new to Java and I am struggling hard obviously.

  10. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Classic Palindrome problem

    To call an instance method you need to use the identifier followed by the dot symbol and the method name. For example if you have a class call A that has a method doIt(). You call it like:

    Java Code:
    ...
    A a = new A();
    a.doIt();
    ...

  11. #11
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Classic Palindrome problem

    ^^thats your answer. So you'd call the method, using the instance.

    palindrome.retrieveInput();

  12. #12
    odd123 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Classic Palindrome problem

    Your problem is that your method, retrieveInput()'s return type is int, while numStr is of type String. That is where your error is coming from.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Classic Palindrome problem

    Quote Originally Posted by odd123 View Post
    Your problem is that your method, retrieveInput()'s return type is int, while numStr is of type String. That is where your error is coming from.
    Wrong. Look again.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Classic Palindrome problem

    There are numerous problems in the posted code.
    DarrylBurke likes this.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Classic Palindrome problem

    PM received from odd123
    Quote Originally Posted by odd123
    Thanks for the constructive criticism, I too am a novice at programming, as you can probably tell. Regarding the palindrome problem, would the error be due to the fact that the retrieveInput method isn't taking in a String parameter?
    Please keep technical questions on the forum. Don't misuse the PM feature.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. palindrome problem
    By Blaedel in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 12:52 AM
  2. Palindrome problem
    By Mika in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:46 PM
  3. Java Classic RPG 2008-04-16
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-17-2008, 06:53 PM
  4. Java Classic RPG 2007-07-19
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-20-2007, 08:57 PM
  5. Java Classic RPG 2007-07-14
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:27 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
  •