How to check if string is a positive integer in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = "8";

        try {
            int n = Integer.parseInt(str);
            
            System.out.println(n >= 0);
        } catch (NumberFormatException e) {
            System.out.println("Not an integer");
        }
    }
}



/*
run:

true

*/

 



answered Jun 25, 2022 by avibootz
edited Jun 25, 2022 by avibootz

Related questions

1 answer 187 views
2 answers 209 views
3 answers 191 views
3 answers 138 views
1 answer 116 views
...