Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Wrong box pops up?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Wrong box pops up?

    I have a code here that initially I would enter a five digit number. If the number is not equal to five an error box pops up. When I plug in a number that is not equal to five it brings up the original box to enter the number, it doesn’t give me the error box. Now when I go and plug a number again that is not equal to 5 then it brings up the error box. So it’s only after the second wrong attempt is when it works correctly.

    I’m new to java coding so I’m sure it’s something simple. Thanks

    public class Palindrome {
     
    	public static void main(String[] args) {
    	display();
    	}
    	private static int getUserInput() {
    	int inputNumber = 0;
    	String answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number");
    	inputNumber = Integer.parseInt(answer);
     
    	while(true) { 
    	    answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number");
    	    if(answer.length() == 5 )  
    	        return Integer.parseInt(answer); 
    	    JOptionPane.showMessageDialog(null,"Please enter a 5 digit number!","Try again", 
    	        JOptionPane.PLAIN_MESSAGE); 
    	}
    	}
    	private static boolean check(){
    	int inputNumber = getUserInput();
    	int number = inputNumber;
    	int[] myArray = new int[5];
    	for (int i = 0; i < myArray.length; i++) {
    	myArray[i] = (int) (number /(Math.pow(10,i)) % 10);
    	}
    	if(myArray[0] == myArray[4] && myArray[1] == myArray[3])
    	return true;
    	else
    	return false;
    	}
    	public static boolean display(){
    	if (check() == true) {
    	JOptionPane.showMessageDialog(null, "This number is a Palindrome",
    	"Excellent!",
    	JOptionPane.INFORMATION_MESSAGE);
    	} else
    	JOptionPane.showMessageDialog(null,
    	"Number is not a Palindrome!",
    	"Sorry",
    	JOptionPane.ERROR_MESSAGE);
    	return false;
    	}
    	}


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Wrong box pops up?

    Your while loop looks ok, but take a closer look at how you are initializing the variables before the loop.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Wrong box pops up?

    It looks like the line
    String answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number");
    should not be there, but I'm not sure. Can you tell me which line is the issue?

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Wrong box pops up?

    Ok, you mentioned that it works correctly after the second attempt.
    Questions you should be asking yourself:
    What is the code that causes the window to pop up and ask for input?
    Is that code unintentionally place elsewhere?
    If it is, how do I stop it from popping up?

    The Mods become angry if I give you the answer directly

  5. The Following User Says Thank You to JJeng For This Useful Post:

    fride360 (March 10th, 2011)

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Wrong box pops up?

    Got it to work, thanks for your help.

Similar Threads

  1. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 03:23 PM
  2. ProgressMonitorInputStream never pops up
    By m2oswald in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 29th, 2010, 11:58 PM
  3. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 08:47 AM
  4. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  5. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM