0
The program takes a numbers only input
If the users enters a letter /alphabets it gets caught and it displays the previously entered number. I want to modify the code so that it takes alphabets only as input. How do. I do that plis help https://code.sololearn.com/coss8Dy9x1an/?ref=app
9 Answers
+ 2
You are reading a string and passing to Integer list by
s = reader.readLine();
result.add(Integer.parseInt(s));
so it raise error.
If you want add alphabets then change the list type....
+ 2
Jayakrishna🇮🇳 yep, your way is better.
+ 1
Jaya raising error when the input is alphabet is fine.
But i want to modify the code so that it receive only alphabets as input and raise error when the user enters non alphabets
+ 1
It reads all as text by readLine(); whether alphabet or numbers..
Raising error is happening when you add to list.
BufferReader only accept input as text..
if you want raise error if input is a number then use Scanner method...
but numbers always can be read as text..
am I understanding wrong anything?
+ 1
Jayakrishna🇮🇳 if I understand correctly (and please, stephen haokip correct me for misunderstanding), the idea would be that the program checks to see if unacceptable characters, in this case non-alpha characters, are input. Then a warning would pop up.
You know how passwords require caps and a number and a special character. This would be that only the alphabet is acceptable.
+ 1
BufferReader has 2 methods :
readLine() which read a entire line of string.
read() which reads a single character and returns it's ascii value.
Both accept input as a text type..
you can read and type cast to needed type..
As you know that "abcde" is a string
And "12345" is also a string
& "@#₹_&+:;!? " is also a string..
All are valid inputs to readLine()
And
a, A, 1,2,&, #, +, .. these are all also single valid character type to be able to read by read() method.
So those accept any input as a text or character types.. Non of " Integer, alphabets, special characters.. " are invalid input types .. So it won't raise error, until no input.
So there, solution would be "accept input and check if it is alphabet or not. " If not raise error otherwise go on..
Hope it make sence, hope I understood your ideas stephen haokip Ausgrindtube
edit
@stephen
read your last post that it actually misleading..
" raise error on alphabet is fine . but you want to raise error on non-alphabet? "
+ 1
I think what I'd do (and I think it's what Jayakrishna🇮🇳 is saying) stephen haokip is put in a Character.isDigit and similar for special characters and iterate through the string, throwing up an error when it's not an alpha.
There might not be any other way because a String is for any character, whereas you can't have an input of nextInt and put a letter in.
+ 1
stephen haokip then I understood correctly.. And I posted the possible ways to do that with Bufferreader.
One way is read character by read method and check by
Character.isLetter(char) ;
if it returns false then raise error manually...
I think, isDigit() also works but it won't check for special chars.. I think it treats alphabets, special characters as both false.. depends on input, you can choose..
0
Jaya... U dont get it it seems.. i have 2 codes... First one in my post... Second one is to be a modified version of the first...
Raise error on alphabet is fine in first code...
And in my modified version it should raise error on non alohabets