• Register
1 vote
38 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?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Well, it is not mandatory to use the scanner class. If you want to take input from the user and check the value whether it is an integer or not you need a scanner class. Otherwise, you can define a variable and check it for an integer number or not! As per your question, I guess you are willing to use a Scanner class to it. let’s do it by using a Scanner class.

Scanner sc = new Scanner(System.in);
    System.out.println("Enter an input here: ");
    if(sca.hasNextInt()){
        System.out.println("Given input is an integer");
        }
        else if(scanner.hasNext()){
            String str = scanner.nextLine();
            if(str.length()>1){
                System.out.println("Given input is a String");
                }
                else{
                    System.out.println("Given input is a Char");
                }
        }

I’ve added all the three processes to check the input type in a single program. The first block will chek for integers and then for string and lastly for chars.

13 9 6
94,240 points

Related questions

0 votes
1 answer 12 views
12 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
1 vote
2 answers 44 views
44 views
Problem: Hello Kodlogs, I am taking an online-based course to learn computer programming. As I am totally new in the business one of my friends suggest me take up the C/C++ programming language (maybe it’s easy to grab) and I did so. Well, I was ... program should ask again to provide an integer number and then it should complete the rest of the operation. Thanks for your co-operation, guys.
asked Apr 29, 2020 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
How can I do this
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: How would you check if a String was a number before parsing it?This is generally done with a simple user-defined function (i.e. Roll-your-own "isNumeric" function). How If it is, we can assume the entire string is numeric:?
asked Feb 15 Ali 4.4k points
0 votes
1 answer 17 views
17 views
I was working on that but I got into some problems.
asked Nov 5, 2020 TeamScript 13.5k points
0 votes
1 answer 16 views
16 views
I was working on that but I got into some problems.
asked Nov 4, 2020 TeamScript 13.5k points
0 votes
1 answer 22 views
22 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?
asked Mar 15, 2020 LizzyM 6.1k points
0 votes
1 answer 3 views
3 views
Problem: Hello! I am trying to write a program that takes input from the user and determines whether the user entered a number, alphabet, or a special character. I am a beginner so, facing some difficulty in understanding the logic. Can somebody from here help me find out the solution? Thanks!
asked 1 day ago Code Learner 5.8k points
0 votes
1 answer 7 views
7 views
Problem: How would you check if a String was a number before parsing it? You can also describe through StringUtils.isNumericSpace which returns true for empty strings and ignores internal spaces in the string. Another way is to use NumberUtils.isParsable which basically checks the number is parsable according to Java. (The linked javadocs contain detailed examples for each method.)
asked Feb 5 Ali 4.4k points
0 votes
1 answer 8 views
8 views
Problem i want know if a string is a number or not
asked Feb 7 ahmedadel9090 2.6k points