Palindrome

5 digit long palindrome

Page 1 of 1

5 Replies - 2733 Views - Last Post: 05 November 2009 - 12:51 AM Rate Topic: -----

#1 khimura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-October 09

Palindrome

Posted 04 November 2009 - 06:24 PM

Basically I'm trying to create a palindrome program that will have the user input a number 5 digits long and is palindrome. I'm a beginner with java and it isn't compiling and running I'm pretty sure that the retrieveInput() method is the problem. I've commented out the num stuff in it but it still doesn't work. I'm at a loss as to what to do next


package hw;

import javax.swing.JOptionPane;

public class Palindrome {
	
	String input;
	public String retrieveInput()
	{
		int num;
		
		input = JOptionPane.showInputDialog("Enter a 5 digit number");
		
		num = Integer.parseInt(input);
		if ((num<100000) && (num>9999))
		{
			check();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "The number entered wasn't within constraints, please reenter the number.", "Error", JOptionPane.ERROR_MESSAGE);
			retrieveInput();
		}
		
		return input;
	}
	public void check()
	{
		char num1;
		char num2;
		char num3;
		char num4;
		
		num1=input.charAt(0);
		num2=input.charAt(1);
		num3=input.charAt(3);
		num4=input.charAt(4);
		
		if ((num1==num3) && (num2==num4))
		{
			display();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "The number entered wasn't a palindrome, please reenter the number.", "Error", JOptionPane.ERROR_MESSAGE);
			retrieveInput();
		}
	}
	public void display()
	{
		JOptionPane.showMessageDialog(null, "The palindrome number you entered was " + input, "Results", JOptionPane.PLAIN_MESSAGE);
	}
	public void main(String args[])
	{
		retrieveInput();
		System.exit(0);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Palindrome

Posted 04 November 2009 - 06:31 PM

did you try declaring your main method as:
public static void main(String[] args){}
//dont forget the static keyword!



if you did, then to call your methods inside main, you will also have to declare them as static.
also, declare your global variables as static as well.
this will compile and run:
import javax.swing.JOptionPane;

public class Palindrome {
	
	static String input;
	public static String retrieveInput()
	{
		int num;
		
		input = JOptionPane.showInputDialog("Enter a 5 digit number");
		
		num = Integer.parseInt(input);
		if ((num<100000) && (num>9999))
		{
			check();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "The number entered wasn't within constraints, please reenter the number.", "Error", JOptionPane.ERROR_MESSAGE);
			retrieveInput();
		}
		
		return input;
	}
	public static void check()
	{
		char num1;
		char num2;
		char num3;
		char num4;
		
		num1=input.charAt(0);
		num2=input.charAt(1);
		num3=input.charAt(3);
		num4=input.charAt(4);
		
		if ((num1==num3) && (num2==num4))
		{
			display();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "The number entered wasn't a palindrome, please reenter the number.", "Error", JOptionPane.ERROR_MESSAGE);
			retrieveInput();
		}
	}
	public static void display()
	{
		JOptionPane.showMessageDialog(null, "The palindrome number you entered was " + input, "Results", JOptionPane.PLAIN_MESSAGE);
	}
	public static void main(String args[])
	{
		retrieveInput();
		System.exit(0);
	}
}



hope it helps :)
Was This Post Helpful? 1
  • +
  • -

#3 khimura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-October 09

Re: Palindrome

Posted 04 November 2009 - 07:01 PM

that worked perfect the program runs, however now i'm getting another error, the program works for palindromes like '11111' or '22222' however not for '12321' or '24442' is it because i coded
char num1;
		char num2;
		char num3;
		char num4;
		
		num1=input.charAt(0);
		num2=input.charAt(1);
		num3=input.charAt(3);
		num4=input.charAt(4);
if ((num1==num3) && (num2==num4))


does this mean it is trying to indirectly link num1 with num4 or num1 with num2?
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Palindrome

Posted 04 November 2009 - 07:17 PM

Lets take an example.
take the num 12321.
so with your variables it would be:
//the number = 12321
		num1=input.charAt(0);// = 1
		num2=input.charAt(1);// = 2
		num3=input.charAt(3);// = 2
		num4=input.charAt(4);// = 1



with your check:
if ((num1==num3) && (num2==num4))


you actually check:
if((1==2) && (2==1))
it is not the way to check a palindrome.
should be:
if ((num1==num4) && (num2==num3))



that would give the desired output :)
Was This Post Helpful? 1
  • +
  • -

#5 khimura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-October 09

Re: Palindrome

Posted 04 November 2009 - 07:37 PM

lol thanks i cant believe i made such simple errors
Was This Post Helpful? 0
  • +
  • -

#6 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Palindrome

Posted 05 November 2009 - 12:51 AM

Just chiming in for a word of encouragement, if all your getting wrong is small simple errors, but your logic is correct then your doing quite well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1