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 3 of 3

Thread: Help making an error message.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help making an error message.

    I am new to programming and need help creating an error message for one of my programs. My question is how do I make java show an error if the user were to enter letters instead of a number and how can I make a loop to keep asking for the input and show the error as long as the input is not a number.

    The following is only part of my code and the only part I want to show an error message for incorrect input.

    	String YearString = JOptionPane.showInputDialog(null,
        			"Please enter a year, for example 1700:",
        			"Finding Easter",
        			JOptionPane.QUESTION_MESSAGE);
     
                    	int YEAR = Integer.parseInt(YearString);

    Thanks in advance to all who helped!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help making an error message.

    My question is how do I make java show an error if the user were to enter letters instead of a number and how can I make a loop to keep asking for the input and show the error as long as the input is not a number.
    A while loop would work in this scenario, looping until the year string validates. Rather than catching the exception, you could simply validate the string. For example in pseudo-code:
     
    String value = "";
    while ( isValid(value) ){
      value = //optionPane input
    }
     
    private boolean isValid(String s){
        //validate the string for digits.
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    Lost_Secret (February 1st, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help making an error message.

    Thank-you for the assistance!

Similar Threads

  1. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  2. How to make an error message when my program crashes?
    By noFear in forum Java Theory & Questions
    Replies: 10
    Last Post: August 11th, 2010, 08:50 AM
  3. an error message while runnung java
    By sravan_kumar343 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 25th, 2010, 10:19 AM
  4. help with a error message
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2010, 02:56 PM
  5. SOAP Message Factory response error
    By Buglish in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 6th, 2008, 01:42 PM