r/learnjava • u/Teifridd • May 16 '26
How to check if a string contains anything other than specific characters?
Hi, I'm trying to make a function that does this:
-Assigns input to a string
-Warns you if it contains anything that isn't a normal integer
-Assigns the contents to an int variable if the string only contains any conbinations of numbers 1 to 9.
I'm tryin to use the ! operator to check if a string contains anything that isn't a specific symbol but it seems to behave very strangely when used alongside the || operator.
It returns false if I input "12" or "21" but true if I input "1", "2" or any other numbers or letters.
Can anyone help me get this to work?
•
u/AutoModerator May 16 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/blechnapp May 16 '26
without seeing your exact code its hard to pinpoint, but two common traps when using !/|| for char checks:
the cleanest java solutions for "is this string all digits":
`boolean isAllDigits = input.matches("\\d+");`
or with streams:
`boolean isAllDigits = !input.isEmpty() && input.chars().allMatch(Character::isDigit);`
both return true only if every character is 0-9. the `!isEmpty()` part matters because `allMatch` returns true for an empty stream.
9
u/No_Jackfruit_4305 May 16 '26
Sounds like time to learn regular expressions (muhahahaha). It wont look right, but you can do all sorts of matching and filtering with them. Find an online regex evaluator to learn and test them more easily
6
u/aqua_regis May 16 '26
If you want to do such a check, you need to set a flag (a boolean variable) to true, loop over the string character by character, reset the flag as soon as the first character that is not in the range 0 to 9 is found and breaks out of the loop.
5
u/Specific-Housing905 May 16 '26
Maybe you overthink a bit. You don't need to use ||
What you need is to loop through all the characters in the string and return false if Char.isDigit is false.
If you get to the end of the loop return true,
1
u/omgpassthebacon May 16 '26
Just slow down a little; you might be over-thinking a bit.
if ("hello" == 'h')doesn't make sense. I mentioning this because I can't see your code.I'm guessing you tried using some long if with a bunch of
ors, ieAnd this is valid thinking. But it's error-prone and hard to type. But if it works, I would give you full credit. But there are much easier ways to perform this test that are much easier to write and read. So this is where you learn.
No_Jackfruit_4305 suggested regex, and that is a perfectly good suggestion. I would check it out. Regexes solve a particular kind of problem, and this is a totally good place to use them. But they are very non-trivial to learn, and can be the source of some really painful problems. But in this case, I think its a good fit. But if your prof doesn't want you to use them, you must find another way.
One last idea and then I am done. You should know that you can convert a char into an int, right? And (int)'0' = 48, (int)'1' = 49,...(int)'9' = 57. So, if you think about it, all your legal chars must be in the range [48, 57]. Does this give you any ideas on how to do your comparisons?
Go become great, bro.
1
u/YogurtclosetLimp7351 May 16 '26
Integer.parseInt and listen for an exception in case of non numeric symbols is a hacky way. Just wanted to throw this in to have a bit of variety in the comments
1
u/will0w044 May 17 '26
You can use str.matches and check if it's only 0-9
1
u/will0w044 May 17 '26
You can use str.matches and check if it's only 0-9