Function IsNumber Learn programming Java

Lesson:

Functions


Exercise:

Function IsNumber


Objetive:

Create a function that tells if a string is an intenger number. It should be used like this:

if (IsNumber ("1234"))
System.Console.WriteLine ("It is a numerical value");


Code:

public class Main
{
	public static boolean IsNumber(String text)
	{
		for (int i = 0; i < text.length(); i++)
		{
		if (text.charAt(i) < '0')
		{
			|| (text.charAt(i) > '9') return false;
		}
		}

		return true;
	}

	public static void main(String[] args)
	{
		System.out.println(IsNumber("12"));
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.