Numeric palindrome

My program stops after user input

Page 1 of 1

13 Replies - 2954 Views - Last Post: 13 September 2009 - 08:26 AM Rate Topic: -----

#1 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Numeric palindrome

Posted 11 September 2009 - 01:19 PM

Hello, I am to wite a program that asks a user for a five digit number, compares the first two digits with the last two and then displays whether or not the number is a palindrome. The requirements for the program is that it contains four methods: main(), retrieveInput(), check(), and the display method.

I think my retrieveInput method is correct since I do get an input box when I try to run the program, I also get an error box if the input is not equal to five numbers. My problem is that after the user inputs the five numbers the program terminates.

I would really appreciate it if someone could tell me what I am doing wrong, or do I have the whole thing messed up. Thank you for any help you are willing to give me, Java is new to me and I am doing my best to understand why my code is or is not working correctly.

This is what I have so far:
import javax.swing.JOptionPane;

public class Palindrome {

	
	
	public static void main(String[] args) {
		
		//Declare variables
		String number;
		
		//Call methods
		number = retrieveInput();}
		
		
		


//The retrieveInput() method asks user to input a five digit integer
	private static String retrieveInput() {
		
		String number = null;
		int numString;
		boolean done = false;
		
		while(!done)
		{
			try
			{
				number = JOptionPane.showInputDialog("Enter five digits ");
				numString = Integer.parseInt(number);
				if (numString <10000 || numString>99999) throw new NumberFormatException();
				else done = true;
			}
			
			catch(NumberFormatException e)
				
			{
				JOptionPane.showMessageDialog(null, "You must enter five digits","Error",JOptionPane.INFORMATION_MESSAGE); 
			}
				
			
		
		}
		return number;
		} 
		
	
	
	public static void check() {
		int number = 0;
		int digit1;
		int digit2;
		
		int digit4;
		int digit5;
		
		
		
		digit5 = number % 10;
		digit4 = (number % 100)/10;
		digit2 = (number % 10000)/1000;
		digit1 = number/10000;
	  
		if(digit1 == digit5 && digit2==digit4);
		{
		  JOptionPane.showMessageDialog( null, "digitString" + "is a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
		}
		
		{
		if (digit1 != digit5 && digit2 != digit4);
		{
		  JOptionPane.showMessageDialog(null, "digitString" + "in not a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
		}
		
		
		
		
	}

}}



Again, Thank You for any guidence offered.

Is This A Good Question/Topic? 0
  • +

Replies To: Numeric palindrome

#2 aks29921  Icon User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: Numeric palindrome

Posted 11 September 2009 - 01:30 PM

reg error if 5 digits are not entered, i think that's what you want....

reg termination of the program after entering 5 digits, what else should it do?
look at your main method, my friend:
public static void main(String[] args) {
	   
		//Declare variables
		String number;
	   
		//Call methods
		number = retrieveInput();}


you have not called the check() method


hope you have your problem solved...
Was This Post Helpful? 1
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Numeric palindrome

Posted 11 September 2009 - 02:02 PM

it terminates coz you never call the check method after you take the input from the user..also from what i see, your check method will be useless since it doesnt have any parameters

import javax.swing.JOptionPane;

public class Palindrome {

   
   
	public static void main(String[] args) {
	   
		//Declare variables
		int number;
	   
		//Call methods
		number = retrieveInput();
	check(number);
}
	   
	   
	   


//The retrieveInput() method asks user to input a five digit integer
	private static int retrieveInput() {
	   
		String number = null;
		int numString = 0;
		boolean done = false;
	   
		while(!done)
		{
			try
			{
				number = JOptionPane.showInputDialog("Enter five digits ");
				numString = Integer.parseInt(number);
				if (numString <10000 || numString>99999) 
			throw new NumberFormatException();
				else 
			done = true;
			}
		   
			catch(NumberFormatException e)
			   
			{
				JOptionPane.showMessageDialog(null, "You must enter five digits","Error",JOptionPane.INFORMATION_MESSAGE);
			}
			   
		   
	   
		}
		return numString;
		}
	   
   
   
	public static void check(int number) {
		
		int digit1;
		int digit2;
	   
		int digit4;
		int digit5;
	   
	   
	   
		digit5 = number % 10;
		digit4 = (number % 100)/10;
		digit2 = (number % 10000)/1000;
		digit1 = number/10000;
	 
		if(digit1 == digit5 && digit2==digit4)
		{
		  JOptionPane.showMessageDialog( null, "digitString" + " is a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
		}
	else
		
		  JOptionPane.showMessageDialog(null, "digitString" + " is not a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
}
		
	   
	  

}



this should do it for you.. compare your code with this and try to understand the differences, if you dont understand anything just ask

This post has been edited by mostyfriedman: 11 September 2009 - 02:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 11 September 2009 - 06:49 PM

View Postaks29921, on 11 Sep, 2009 - 12:30 PM, said:

reg error if 5 digits are not entered, i think that's what you want....

reg termination of the program after entering 5 digits, what else should it do?
look at your main method, my friend:
public static void main(String[] args) {
	   
		//Declare variables
		String number;
	   
		//Call methods
		number = retrieveInput();}


you have not called the check() method


hope you have your problem solved...


Thank you, this is my first time working with methods. I should have realized that the program would terminate after the only method I called. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#5 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 11 September 2009 - 07:00 PM

View Postmostyfriedman, on 11 Sep, 2009 - 01:02 PM, said:

it terminates coz you never call the check method after you take the input from the user..also from what i see, your check method will be useless since it doesnt have any parameters

import javax.swing.JOptionPane;

public class Palindrome {

   
   
	public static void main(String[] args) {
	   
		//Declare variables
		int number;
	   
		//Call methods
		number = retrieveInput();
	check(number);
}
	   
	   
	   


//The retrieveInput() method asks user to input a five digit integer
	private static int retrieveInput() {
	   
		String number = null;
		int numString = 0;
		boolean done = false;
	   
		while(!done)
		{
			try
			{
				number = JOptionPane.showInputDialog("Enter five digits ");
				numString = Integer.parseInt(number);
				if (numString <10000 || numString>99999) 
			throw new NumberFormatException();
				else 
			done = true;
			}
		   
			catch(NumberFormatException e)
			   
			{
				JOptionPane.showMessageDialog(null, "You must enter five digits","Error",JOptionPane.INFORMATION_MESSAGE);
			}
			   
		   
	   
		}
		return numString;
		}
	   
   
   
	public static void check(int number) {
		
		int digit1;
		int digit2;
	   
		int digit4;
		int digit5;
	   
	   
	   
		digit5 = number % 10;
		digit4 = (number % 100)/10;
		digit2 = (number % 10000)/1000;
		digit1 = number/10000;
	 
		if(digit1 == digit5 && digit2==digit4)
		{
		  JOptionPane.showMessageDialog( null, "digitString" + " is a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
		}
	else
		
		  JOptionPane.showMessageDialog(null, "digitString" + " is not a palindrome", "digitString", JOptionPane.PLAIN_MESSAGE);
}
		
	   
	  

}



this should do it for you.. compare your code with this and try to understand the differences, if you dont understand anything just ask


This is the second time this week you have come to my rescue. I am deeply in your debt. I had to change a couple things such as my display code but with your help my program works perfectly now. I'm not sure if I quite understand the method processes yet but for the life of me I can't think of any intelligent questions to ask yet. Again, thank you very much- 2 assignments down 1 more to go before it starts all over on Monday.

This post has been edited by Vanessa6: 11 September 2009 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Numeric palindrome

Posted 11 September 2009 - 07:04 PM

no problem ;), is there anything specific that you dont understand?
Was This Post Helpful? 0
  • +
  • -

#7 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 11 September 2009 - 07:28 PM

View Postmostyfriedman, on 11 Sep, 2009 - 06:04 PM, said:

no problem ;), is there anything specific that you dont understand?

Honestly I wish I could narrow my questions down to something specific, I'm not sure if I'm just not understanding any of this or if I'm just making it more complicated that it really is. I'm getting ready to start on my last assignment for this week and it uses methods also so I guess we'll see how far I get with it before I'm back on here posting again, hopefully some of this is sinking in.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Numeric palindrome

Posted 11 September 2009 - 09:31 PM

and the body of each method is between {}

	public static void main(String[] args) {
	....
	}

	public static xxx() {
	....
	}

	public static yyy() {
	....
	}

not as you did

	public static void main(String[] args) {
	....
	

	public static xxx() {
	....
	

	public static yyy() {
	...
	}
  }
}


Was This Post Helpful? 1
  • +
  • -

#9 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 11 September 2009 - 09:36 PM

View Postpbl, on 11 Sep, 2009 - 08:31 PM, said:

and the body of each method is between {}

	public static void main(String[] args) {
	....
	}

	public static xxx() {
	....
	}

	public static yyy() {
	....
	}

not as you did

	public static void main(String[] args) {
	....
	

	public static xxx() {
	....
	

	public static yyy() {
	...
	}
  }
}


Thank you for pointing that out, just one more thing that I missed.
Was This Post Helpful? 0
  • +
  • -

#10 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: Numeric palindrome

Posted 12 September 2009 - 04:39 AM

Here is another way of implementing the above problem. By just initializing the input into String data type, We can now switch the place of either the first pair (1st and second digit) or the second pair (2nd to the last and the last digit) to know if the number is a palindrome.

import javax.swing.*;
public class Palindrome{
	static String num = "";
	public static void main(String[] args){
		if(retrieveInput()){
			check(num);
		}
	}

	public static boolean retrieveInput(){
		num = JOptionPane.showInputDialog(null, "Enter a number of length 5:");
		if(num.length() < 5){
			JOptionPane.showMessageDialog(null, num + "'s length is smaller than 5. Please follow the correct format (e.g: 56763)");
			return false;
		}else{
			JOptionPane.showMessageDialog(null, num + " is in a correct format.");
			return true;
		}
	}

	public static void check(String num){
		String a = "", b = "";
		a = num.charAt(1) + "" + num.charAt(0);
		b = num.substring((num.length() - 2),(num.length()));
		if(a.equals(b)){
			JOptionPane.showMessageDialog(null, num + " is a palindrome");
		}else{
			JOptionPane.showMessageDialog(null, num + " is not a palindrome");
		}
	}
}


This post has been edited by krizjaz: 12 September 2009 - 04:44 AM

Was This Post Helpful? 1
  • +
  • -

#11 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 12 September 2009 - 09:30 AM

View Postkrizjaz, on 12 Sep, 2009 - 03:39 AM, said:

Here is another way of implementing the above problem. By just initializing the input into String data type, We can now switch the place of either the first pair (1st and second digit) or the second pair (2nd to the last and the last digit) to know if the number is a palindrome.

import javax.swing.*;
public class Palindrome{
	static String num = "";
	public static void main(String[] args){
		if(retrieveInput()){
			check(num);
		}
	}

	public static boolean retrieveInput(){
		num = JOptionPane.showInputDialog(null, "Enter a number of length 5:");
		if(num.length() < 5){
			JOptionPane.showMessageDialog(null, num + "'s length is smaller than 5. Please follow the correct format (e.g: 56763)");
			return false;
		}else{
			JOptionPane.showMessageDialog(null, num + " is in a correct format.");
			return true;
		}
	}

	public static void check(String num){
		String a = "", b = "";
		a = num.charAt(1) + "" + num.charAt(0);
		b = num.substring((num.length() - 2),(num.length()));
		if(a.equals(b)){
			JOptionPane.showMessageDialog(null, num + " is a palindrome");
		}else{
			JOptionPane.showMessageDialog(null, num + " is not a palindrome");
		}
	}
}


Thanks, It's very helpful to see an alternative way of arriving at the solution. I'm sure that this will help me in understanding the concepts once I go through and understand the logic :)
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Numeric palindrome

Posted 12 September 2009 - 10:29 AM

this solution will work with any string of length 5 though..so if the user enters a string that doesnt contains number it will still work..for more accurate result you need to check that the string contains only numbers
Was This Post Helpful? 1
  • +
  • -

#13 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: Numeric palindrome

Posted 12 September 2009 - 10:34 AM

View Postmostyfriedman, on 12 Sep, 2009 - 09:29 AM, said:

this solution will work with any string of length 5 though..so if the user enters a string that doesnt contains number it will still work..for more accurate result you need to check that the string contains only numbers

I would have missed that, thanks.
Was This Post Helpful? 0
  • +
  • -

#14 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: Numeric palindrome

Posted 13 September 2009 - 08:26 AM

Ok, if you want the complete code, here it is. I just posted another way of implementing the problem, not solve the problem completely. mostyfriedman, i'm not here to compete with you. I just want to share my thoughts and ways. Still, my code works and it is correctly done. Here is what you called the "accurate result":
import javax.swing.*;
public class Palindrome{
	static String num = "";
	public static void main(String[] args){
		if(retrieveInput()){
			check(num);
		}
	}

	public static boolean retrieveInput(){
		num = JOptionPane.showInputDialog(null, "Enter a number of length 5:");
		boolean cont = true;
		boolean toReturn = true;
		for(int i = 0; i < num.length(); i++){
			if(!Character.isDigit(num.charAt(i))){
				cont = false;
				JOptionPane.showMessageDialog(null, "Please enter numbers only");
				break;
			}
		}
		if(cont){
			if(num.length() < 5){
				JOptionPane.showMessageDialog(null, num + "'s length is smaller than 5. Please follow the correct format (e.g: 56763)");
				toReturn = false;
			}else{
				JOptionPane.showMessageDialog(null, num + " is in a correct format.");
			}
		}else{
			toReturn = false;
		}
		return toReturn;
	}

	public static void check(String num){
		String a = "", b = "";
		a = num.charAt(1) + "" + num.charAt(0);
		b = num.substring((num.length() - 2),(num.length()));
		if(a.equals(b)){
			JOptionPane.showMessageDialog(null, num + " is a palindrome");
		}else{
			JOptionPane.showMessageDialog(null, num + " is not a palindrome");
		}
	}
}


Hope this is accurate for you now. Thanks for the comment.

This post has been edited by krizjaz: 13 September 2009 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1