How to check if string is a negative integer in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = "-13";
 
        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

Related questions

1 answer 158 views
1 answer 181 views
2 answers 120 views
1 answer 113 views
1 answer 102 views
...