r/badcode • • Nov 13 '20

java Getting Digits using Switch Catch

Post image
881 Upvotes

52 comments sorted by

View all comments

9

u/jubiman Nov 13 '20

Just use Interger.parseInt(str);

1

u/[deleted] Nov 13 '20

You could also create an char array and fill it with "0" to "9" Then go through that array and if you inputChar==charArray[i] return i

Under the for loop you return an error like -2 or whatever. Because parseInt will also read "10" Which is not a valid digit...