• Register
0 votes
19 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 8 views
8 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 4 views
4 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 5.6k points
0 votes
1 answer 10 views
10 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 33 views
33 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, 2020 LizzyM 6.1k points
0 votes
1 answer 16 views
16 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 15 views
15 views
I was working on that but I got into some problems.
asked Nov 5, 2020 TeamScript 9.2k points
0 votes
1 answer 13 views
13 views
I was working on that but I got into some problems.
asked Nov 4, 2020 TeamScript 9.2k points
1 vote
1 answer 29 views
29 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, 2020 Gavin 15.3k points
0 votes
1 answer 9 views
9 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 4 views