Results 1 to 15 of 15
Thread: Classic Palindrome problem
- 05-12-2012, 04:31 AM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
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.
- 05-12-2012, 04:36 AM #2
Re: Classic Palindrome problem
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-12-2012, 06:35 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 05-13-2012, 12:26 AM #4
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Classic Palindrome problem
So I have to make a new palindromeMethod object? Where do I insert that?
- 05-13-2012, 01:27 AM #5
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Classic Palindrome problem
Yes, you have to create a PalindromeMethod object and you can do it in the main() method of your class.
Website: Learn Java by Examples
- 05-13-2012, 01:44 AM #6
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Classic Palindrome problem
Main meaning:
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"); }
Does this look correct?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(); }

Still getting errors as you can see :(
- 05-13-2012, 01:48 AM #7
- 05-13-2012, 10:29 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
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?
- 05-13-2012, 10:15 PM #9
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
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.
- 05-14-2012, 02:15 AM #10
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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(); ...
Website: Learn Java by Examples
- 05-14-2012, 03:58 AM #11
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Re: Classic Palindrome problem
^^thats your answer. So you'd call the method, using the instance.
palindrome.retrieveInput();
- 05-14-2012, 04:01 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
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.
- 05-14-2012, 06:13 AM #13
- 05-14-2012, 06:20 AM #14
Re: Classic Palindrome problem
There are numerous problems in the posted code.
- 05-14-2012, 10:45 AM #15
Similar Threads
-
palindrome problem
By Blaedel in forum New To JavaReplies: 5Last Post: 10-03-2009, 12:52 AM -
Palindrome problem
By Mika in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:46 PM -
Java Classic RPG 2008-04-16
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-17-2008, 06:53 PM -
Java Classic RPG 2007-07-19
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-20-2007, 08:57 PM -
Java Classic RPG 2007-07-14
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-14-2007, 08:27 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks