How To Check If User Input is a Number or Not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How To Check If User Input is a Number or Not

I have a program that checks whether the user input is greater than 8. I want it to have a response for every situation, even if the user enters in words. How to I check whether their input is words and then change it to another number, like 14 so it can go through the rest of the program? https://code.sololearn.com/c4V09YHZb4ng/?ref=app

18th Sep 2018, 10:47 PM
Sir White Cat
Sir White Cat - avatar
4 Answers
+ 11
n = input("Enter a number: ") if not n.isdigit(): print("You didn't enter a number!")
18th Sep 2018, 11:20 PM
Eduardo Petry
Eduardo Petry - avatar
+ 1
import java.util.Scanner; public class Numb{ public static void main(String args[]){ boolean cond=true; string i=“”; Exception e2=null; while(cond){ try{ System.out.printIn(“Enter Digits ONLY”); i=new Scanner(System.in).next(); for(int a=0; a<i.length();++a) if(!Charater.isDigit(i.charAt(a)))throw e2;; cond=false; } catch(Exception e){ System.out.printIn(“Numbers only,please”); }} System.out.printIn(“following is only numbers\n”+i); }} // this is another approach
19th Sep 2018, 9:30 AM
thulani joyisa
thulani joyisa - avatar
0
Flamibat ...n=int(input(" enter a number")) if n.isdigit()==True: Print("number")
19th Sep 2018, 5:27 PM
Sri Raman Prasad
- 1
3 nmb
20th Sep 2018, 8:32 PM
Tamás Filep
Tamás Filep - avatar