Words with numbers

  • 12
  • Locked
The main method's first parameter is file1 and the second is file2. file1 contains lines with words separated by spaces. Write to file2 all the words that contain numbers, for example, a1 or abc3d. Separate the words with spaces. Close the streams.
You can't complete this task, because you're not signed in.
Comments (10)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Олег Байбула
Level 32 , Ukraine
Expert
6 March, 10:12
The regex in "correct solution" is wrong. It wouldn't match "a1" or any String, that ended with digit.
Justin Smith
Level 38 , Greenfield, USA, United States
9 November 2021, 13:54
This task is where you wish String.contains had a version that took a regex argument.
Олег Байбула
Level 32 , Ukraine
Expert
6 March, 09:55
It would be needless.
Mike S
Level 28 , Saint Louis, United States
17 March, 13:27
So, I tried this... String.contains("[0-9]") and it didn't filter out non-digit containing words. Why is that?
Petros
Level 23 , Columbia, United States
28 August 2020, 17:02
Stack overflow saved me again:
word.matches(".*\\d.*")
This code will return true for a string that contains any number whatsoever. Sorry, didn't come up with another way on my own. This code works too well.
Justin Smith
Level 38 , Greenfield, USA, United States
9 November 2021, 14:05
EDIT: Nevermind, the problem wasn't your regex, it was another part of my code!
BlueJavaBanana
Level 37
6 June 2020, 16:12
Depressing task. I love code gym but sometimes when you waste hours and hours because your code works as it asks but the verification fails it makes you want to quit. I cannot imagine what tasks at level 30 and above are like.
Tony Roy
Level 41 , Victorville, United States
2 April 2020, 15:45
A general suggestion: Comments suggest that quite a few students become frustrated thinking their code works but will not pass verification. Many times, it happens that the verification is good, and there is some test that we have not thought of (although sometimes I have never been able to identify the problem and get it to verify one way but not another). In this exercise, I thought I had a solution, but Guadalupe Gagnon kindly gave me a case to expose an error. Perhaps it would be worthwhile to supply such cases with verification. It is much easier to fix a problem when you know there is one, than when you think there is not. This might reduce frustration (and attrition). Let me say, though that I very much appreciate the instant feedback on exercises from the verification -- this is only a thought for improvement.
Andrei
Level 41
19 February 2021, 11:47
Let me ask you this. In real life, how would you find out if your program is working correctly or not ? Would you have a test scenario ready somewhere or would you have to figure it out on your own or with the help of a more experienced colleague? I believe, although frustrating, this is a way of making us think of all of the possible scenarios. If you want to try something with scenarios already provided I recommend codingame.
Radek
Level 22 , Warsaw, Poland
21 November 2018, 12:39
There is an error in assumption or validator. The validator expects pure numbers to be written to the output file, too. Solution done according to the conditions should write to the output file any string containing at least one letter and at least one digit. Such solution does not pass the validation. A string without any letters cannot have a lexical meaning, cannot be a word.