Can someone help me with error trapping. For instance, if I asked the user to input only a numeric type, 0-9. Then how would I go about creating an error message if the user inputs "hey." I know that I could easily use the Try, Catch statements but I want to learn the other ways.
BuhRock 0 Light Poster
Recommended Answers
Jump to PostInstead of using Character.isDigit( char ), you could use the matches() method from the String class. This would save having to test the length of the data and having to convert it to a char.
import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { …
Jump to PostI believe the regular expression for that would be: [0-9]|-[0-9]. A more readable test would be:
if ( input.matches( "[0-9]" ) || input.matches( "-[0-9]" ) ) { JOptionPane.showMessageDialog(null, "Correct input."); } else JOptionPane.showMessageDialog(null, "Incorrect input!");
I am no expert in regex though, and there are better ways …
All 6 Replies
BestJewSinceJC 700 Posting Maven
BuhRock 0 Light Poster
BestJewSinceJC 700 Posting Maven
chaospie 0 Newbie Poster
BuhRock 0 Light Poster
chaospie 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.