Java code to check given String is Numeric or not example

This tutorial shows you how to check given string is numeric or not using java code. Suppose you need to calculate the sum of 3 marks (integer only). For calculating sum of the 3 marks , you may need to accept 3 marks as input through console. Java accepts marks as a string, so these string numbers has to be converted to integer before calculation. Before that we may check that the input strings contains only digits ( i.e. the input string is Numeric or not ). This can be done in many ways. Let us do in two ways. In first method we have written own function isNumeric() to check each characters is digit or not using Charater.isDigit(). In the Second method, using NumberFormatException by putting the Integer.parseInt(str) in a try..catch block. if the input mark is not a valid numeric means it will throw error. The java code to check the given string is numeric or not is given below

Output of the above program ::

numeric validation numeric validation1

 

 

You may also like

Leave a Reply