Java : Data validation

Now that you’re probably already coding, let’s take it to the next level – let’s make sure you’re doing it right!

So you must’ve heard the term data validation before. You didn’t? It’s what you see almost at any form you fill in the internet, for example, email address must be proper, you can’t just enter a random number as a credit card number, your cvv number MUST be 3 digits (be careful where you’re entering it, though!), your mobile number can’t have a letter in it…. And it goes on and on! What’s this thing which is not allowing you to spam the systems with completely rubbish data? It’s data validation! Data validation is nothing but checking if the user entered the correct thing at the correct place. Yes there are people who will enter their names when you ask for their grades and this will only spoil your fool proof program! So you must ensure you’ve a robust data validation system in place.

Here are some tips –

• Use a do while loop to get input, repeating until the correct type of input is not received from the user

• Use try catch blocks to handle errors!

• Discard the incorrect input immediately and ask the user to replace it with a proper one.

• Use proper prompts for input!

• Make sure you handle border line cases well!

For example, if designing a program to check for prime numbers, check first if the number is positive. If not, discard the input and ask for another input/end the program. Always check all conditions before starting the calculations!


So what do you want to learn next in Java? Share in the comments below!

Featured image – Choosing (coding) the right path. © Yashovardhan Dhanania, 2017

Advertisements

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s