• Register
1 vote
50 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,260 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
1 vote
2 answers 66 views
66 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 16 views
16 views
How can I do this
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 26 views
26 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 Feb 25 Code Learner 9.9k points
0 votes
1 answer 3 views
3 views
Problem: I am getting this .. can you help me to solve it? How to check if input is a number python
asked May 1 Ifra 37.2k points
1 vote
1 answer 28 views
28 views
Problem: Hello good people, I am pretty much new in the programming sector. I recently started learning C, and C++. Reading a textbook is hard for me from my childhood. For the obvious reason, I decided to learn from the internet. I am solving basic problems from ... me out to write such a program that checks if the input is a number? I am looking forward to having your solutions. Many thanks.
asked Apr 12, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem I have a gpa program, How to check if a string is numeric? I need help! if anybody faced this situation please give me the solution
asked Apr 9 neeraj 10.5k points
0 votes
1 answer 19 views
19 views
I was working on that but I got into some problems.
asked Nov 5, 2020 TeamScript 13.5k points
0 votes
1 answer 17 views
17 views
I was working on that but I got into some problems.
asked Nov 4, 2020 TeamScript 13.5k points
0 votes
1 answer 25 views
25 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