Please, How I check if the string digits “number” or not ? “Java” | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please, How I check if the string digits “number” or not ? “Java”

I have a string variable enters from console because I need check the length but in the same time I want check if it digit or not

2nd Nov 2019, 10:26 AM
Renad Al-Garni
Renad Al-Garni - avatar
3 Answers
+ 1
You need to make a Boolean function which return true if string you entered can parse to any other numeric data type. Like this: public static Boolean checkstr(String str) try{ Double.parseDouble(str); return True; } catch(Exception e) { Return False } Do this after you find the length of string. Because this function will convert your string to number and then you can't use .length() method on your string. :)
2nd Nov 2019, 10:54 AM
Chirag Kumar
Chirag Kumar - avatar
0
myObject.getClass().getName(); // Its show type of your object.
2nd Nov 2019, 3:23 PM
Leonardo Sartori
Leonardo Sartori - avatar
0
Did you mean you want to check each character in the string for being a digit (0-9)? Why you tagged 'iostream'? isn't that C++ header file?
2nd Nov 2019, 3:52 PM
Ipang