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
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?
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
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.
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.
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
Code java:
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.
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.
Re: How to make an error message when my program crashes?
Quote:
Originally Posted by
helloworld922
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.
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?
Re: How to make an error message when my program crashes?
Code Java:
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.");
}
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.
Re: How to make an error message when my program crashes?
Quote:
Originally Posted by
Norm
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?
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