Java – Learn & Share http://zulkifliishak.com A blog where I learn and Share Mon, 06 Aug 2018 00:18:35 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 149499638 How to check a string is number in java using regular expression http://zulkifliishak.com/how-to-check-a-string-is-number-in-java-using-regular-expression/ Mon, 06 Aug 2018 00:18:07 +0000 http://zulkifliishak.com/?p=52 Checking whether a string is number or not can be very useful in certain application. In Java there are various way to check whether a number is a string or not. We can use the Exception , try .. catch block, checking character by character or by using regular expression. 

Compared to all the methods, checking whether a string is number or not by using regular expression is basically the easiest.

The comparison is based on pattern matching, regular expression is used to create the pattern and is compared to the string. If the pattern match it will return TRUE, if the pattern mismatch it will return FALSE. 


1
2
3
4
5
6
7
8
public static boolean isNumeric(String token) {
String regex,regex1,regex2;
    regex = "[-+]?[0-9]+.?[0-9]+";
    regex1 = "[-+]?[0-9]+.?";
    regex2 = "[-+]?.?[0-9]+";
return token.matches(regex)|token.matches(regex1)|token.matches(regex);
}
}

]]>
52
Printing Hello World in Java http://zulkifliishak.com/printing-hello-world-in-java/ Mon, 16 Jul 2018 09:43:13 +0000 http://zulkifliishak.com/?p=25  

1
2
3
4
5
6
public class HelloWorld
{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Above program is example of how to print hello world in Java.

To print out, java print method is used.

 System.out.println("Hello World!");

println will print the string “Hello World” together with a new line.

 

]]>
25