Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,884 answers

573 users

How to check if a string is numeric in Java

4 Answers

0 votes
public class MyClass {
    public static boolean isNumeric(String s) {
	    if (s == null) {
	        return false;
	    }
	    try {
	        double d = Double.parseDouble(s);
	    } catch (NumberFormatException e) {
	        return false;
	    }
	    return true;
	}
    
    public static void main(String args[]) {
        System.out.println((isNumeric("989762") == true) ? "yes" : "no");
        System.out.println((isNumeric("3.14") == true) ? "yes" : "no");
        System.out.println((isNumeric("-298") == true) ? "yes" : "no");
        System.out.println((isNumeric("12W") == true) ? "yes" : "no");
    }
}



/*
run:

yes
yes
yes
no

*/

 



answered Jul 25, 2020 by avibootz
0 votes
import java.util.regex.Pattern;

public class MyClass {
    private static Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
    
    public static boolean isNumeric(String s) {
	    if (s == null) {
	        return false;
	    }
	    return pattern.matcher(s).matches();
	}
    
    public static void main(String args[]) {
        System.out.println((isNumeric("989762") == true) ? "yes" : "no");
        System.out.println((isNumeric("3.14") == true) ? "yes" : "no");
        System.out.println((isNumeric("-298") == true) ? "yes" : "no");
        System.out.println((isNumeric("12W") == true) ? "yes" : "no");
    }
}



/*
run:

yes
yes
yes
no

*/

 



answered Jul 25, 2020 by avibootz
0 votes
public class MyClass {
    public static boolean isNumeric(String str) {
        boolean isNumeric = true;
         
        try {
            Double.parseDouble(str);
        } catch (NumberFormatException e) {
                isNumeric = false;
        }
         
        return isNumeric;
    }
     
    public static void main(String args[]) {
        String str = "203874";
         
        System.out.println(isNumeric(str));
    }
}
     
     
     
     
/*
run:
     
true
     
*/

 



answered Nov 30, 2023 by avibootz
0 votes
public class MyClass {
    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");
    }
    
    public static void main(String args[]) {
        String str = "203874";
        
        System.out.println(isNumeric(str));
    }
}
    
    
    
    
/*
run:
    
true
    
*/

 



answered Nov 30, 2023 by avibootz

Related questions

2 answers 132 views
2 answers 124 views
2 answers 116 views
3 answers 184 views
3 answers 165 views
3 answers 230 views
3 answers 139 views
139 views asked Mar 28, 2023 by avibootz
...