• Register
0 votes
17 views

I was doing my java programming practice. I wrote a code to check if a charcter is a number:

   public static void main(String[] args) {

        String string = "-1234.15";
        String numeric ;

        numeric = string.matches("-?\\d+(\\.\\d+)?");

        if(numeric)
            System.out.println(string + " is a number");
        else
            System.out.println(string + " is not a number");
    }

But it gives me error. Whats wrong in my code?

10 7 2
6,050 points

1 Answer

0 votes
For this, Java has a boolean data type, which can take the values as True or False, A boolean type is declared with the boolean keyword and can only take the values True or False. However, it is more common to return boolean values from boolean expressions, for conditional testing (see below):
 public static void main(String[] args) {

        String string = "-1234.15";
        boolean numeric = true ;

        numeric = string.matches("-?\\d+(\\.\\d+)?");

        if(numeric)
            System.out.println(string + " is a number");
        else
            System.out.println(string + " is not a number");
    }

Happy coding.

11 6 4
34,930 points

Related questions

0 votes
1 answer 2 views
2 views
I need to check if a number is a perfect squre in java.
asked Oct 5 Daniel Anderson 4k points
0 votes
1 answer 28 views
28 views
I am very new at java programing, I have an assginment where I have to check if a character is uppercase in java and I wrote a code: public static void main(String[] args) { int A; char a = A; if (Character.isUpperCase()) { System.out.println(a + " is an uppercase character."); } } But it doesn’t work how. Can anyone help me out?
asked Mar 15 LizzyM 6.1k points
0 votes
1 answer 7 views
7 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25 Daniel Anderson 4k points
1 vote
1 answer 22 views
22 views
Problem: Hello good people, I am a novice in java programming. I am learning java online and solving problems daily. Today I got a problem that says to check if input is a number. How can I check it? Is it require a scanner class? Could anybody here help me to solve this problem, please?
asked Apr 7 Gavin 15.3k points
0 votes
1 answer 7 views
7 views
I am finding the answers in several blog for removing particular character from string in java.
asked Sep 29 Daniel Anderson 4k points