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

Thread: How to make an error message when my program crashes?

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make an error message when my program crashes?

    Hello
    Here is the situation: I have a variable from type int. I read it from the console. When letters are read instead of numbers the program crashes. I need a message when the program crashes-to make the crash more beautiful For example the message can be "You must enter a number not text".
    Thanks in advance


  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: How to make an error message when my program crashes?

    Please post what you have so far. Usually something simple such as checking if its a number, and if not print out your error will do the trick. Please also explain what you mean by 'crash' - do you mean an exception is thrown?

  3. #3
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make an error message when my program crashes?

    I have nothing so far. Here is a better explanation. I have this code:

    System.out.println("Enter RAM");
    int entered_RAM=scan.nextInt();

    I should enter a number here. When i enter letters the program crashes:

    Enter RAM
    asd
    Error: null
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Specification.main(Specification.java:162)

    Instead of this I need a simple line telling :You should enter a number

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make an error message when my program crashes?

    Two solutions:

    If you are using Integer.parseInt(string), then it will automatically detect this problem and throw an exception. All you have to do is catch it and display the message.

    try
    {
         Integer.parseInt("hello");
    }
    catch(Exception e)
    {
        System.err.println("You didn't enter a number.");
    }

    The second alternative is to check before-hand that the input is indeed a valid number. This takes a little more code on your part, but this method is generally faster because throwing/catching exceptions is kind of slow (negligible if the exceptions are only going to be thrown once or twice every second, but it does stack up if you try to throw too many exceptions quickly).

    edit:

    you can also catch the exception Scanner.nextInt() is throwing.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to make an error message when my program crashes?

    Would a try and catch statement work? It is not the neatest of way, but it works

    System.out.println("Enter RAM");
    int entered_RAM;
    try{
    entered_RAM=scan.nextInt();
    }catch(Exception ex){ System.out.println("You should enter a number"); entered_RAM=scan.nextInt();};

    That will stop you from getting exceptions, and it should get the next input, but it wont check a second time if that value is still invalid. To keep checking until a good answer is given, you would have to loop or something.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make an error message when my program crashes?

    Another problem with using Scanner is that the invalid data is still in the buffer. The next nextInt() call will again try to make an int and will fail. You need to clear what is in the buffer before trying to use nextInt() again.
    The next() method will read what is next in the buffer but leave the rest of the line that was input, possibly only a newline char.
    The nextLine() will read all that was typed in including the newline char.

    You need to experiment with using these methods. Read into a String and print it out to see what is happening.

  7. #7
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make an error message when my program crashes?

    Quote Originally Posted by helloworld922 View Post
    Two solutions:

    If you are using Integer.parseInt(string), then it will automatically detect this problem and throw an exception. All you have to do is catch it and display the message.

    try
    {
         Integer.parseInt("hello");
    }
    catch(Exception e)
    {
        System.err.println("You didn't enter a number.");
    }

    The second alternative is to check before-hand that the input is indeed a valid number. This takes a little more code on your part, but this method is generally faster because throwing/catching exceptions is kind of slow (negligible if the exceptions are only going to be thrown once or twice every second, but it does stack up if you try to throw too many exceptions quickly).

    edit:

    you can also catch the exception Scanner.nextInt() is throwing.
    I really can`t understand how to use this code...can you put mine code in yours to see the usage?

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make an error message when my program crashes?

    Scanner reader = new Scanner(System.in);
    System.out.println("ram available: ");
    int ram = 0;
    try
    {
         ram = Integer.parseInt(reader.next());
    }
    catch(Exception e)
    {
        System.err.println("You didn't enter a number.");
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make an error message when my program crashes?

    Another approach is to use some of Scanner class's hasNext... methods. You can ask the Scanner object if the next thing in its buffer to be read is an int by using the hasNextInt() method. If what's there is NOT what you want, you can read it to clear the buffer by using the next or nextLine method, then tell the user about his incorrect input and ask him to try again.
    Warning, many of the Scanner methods will block waiting until the user enters something.

  10. #10
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make an error message when my program crashes?

    Quote Originally Posted by Norm View Post
    Another approach is to use some of Scanner class's hasNext... methods. You can ask the Scanner object if the next thing in its buffer to be read is an int by using the hasNextInt() method. If what's there is NOT what you want, you can read it to clear the buffer by using the next or nextLine method, then tell the user about his incorrect input and ask him to try again.
    Warning, many of the Scanner methods will block waiting until the user enters something.
    I like this approach very much can you give me a simple example how to use that?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make an error message when my program crashes?

    In pseudo code:
    loop until good data type
    Ask user for data
    use hasNext to test if desired data type ready to be read
    if it is, exit the loop
    Use nextLiine() to read in the wrong data
    Tell user s/he entered the wrong data
    end loop

Similar Threads

  1. Enforce DTD on DomParser - Make Parser fall over if error detected
    By toriacht in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 18th, 2010, 05:28 PM
  2. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  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