user3203580
user3203580

Reputation: 1

Code doesn't allow the user to input letters

So far I have a code that asks for a user input but a part of my code isn't accepting letters as inputs. For example if i type in say woah123 it'll give me a number format exception. Any way to get around this? Error is at the second line int i = Integer.parseInt(sentence). Sentence is the user input

sentence.replaceAll("\\D", "");
int i = Integer.parseInt(sentence);
i = i * 2 ;
woah.replaceAll("\\d", "" + i);
System.out.println(woah);

Upvotes: 0

Views: 191

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

Strings are immutable.

Generally, every modification you made on an immutable object will "give" you another immutable object.

So it should be :

sentence = sentence.replaceAll("\\D", "");

Indeed you have to do the same for woah.

You may read about what is an immutable object.

Upvotes: 6

Related Questions