• Register
0 votes
23 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

0 votes
1 answer 17 views
17 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
Problem: we are going to figure out how many times a word is repeated in the text file using a map interface method through a Java program. I don’t know how to count the number of occurrences of a word in a text file using a hash-map then you are at the right place to know your problem’s solution?
asked Dec 2, 2020 Mashhoodch 13k points
0 votes
1 answer 13 views
13 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 18 views
18 views
I am finding the answers in several blog for removing particular character from string in java.
asked Sep 29, 2020 Daniel Anderson 4k points
0 votes
1 answer 26 views
26 views
Problem: Hi there! I just got an assignment in which I had to remove the first and last character from a string in Java. I did the task using substrings. I want to know what the simplest way is to do so? Also, mention other alternatives to using the substring method. Thanks!
asked Jan 8 Code Learner 9.2k points
0 votes
1 answer 2 views
2 views
Problem: anyone who know how to check if string contains substring java? please help. How to check if string contains substring java
asked Apr 4 Rohit kr 16.2k points
0 votes
1 answer 73 views
73 views
I am finding the answers how to check string contains special characters in Java.
asked Sep 26, 2020 Daniel Anderson 4k points
0 votes
1 answer 13 views
13 views
I am finding difficulty how to check radio button is checked applying Jquery. Is there any one here give me the hint?
asked Sep 25, 2020 Daniel Anderson 4k points
1 vote
1 answer 2 views
2 views
Problem: How to check the following? Java check if string contains special characters.
asked Apr 2 PkGuy 13.1k points