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.
Requirements:
The program must NOT read data from the console.
The program must read the first file's contents (use the FileReader constructor with a String parameter).
The file input stream (FileReader) must be closed.
The program should write all the words containing numbers from the first file to the second file (use FileWriter).
The file output stream (FileWriter) must be closed.
package com.codegym.task.task19.task1923;
/*
Words with numbers
*/
public class Solution {
public static void main(String[] args) {
}
}
I was avoiding regular expressions for a long time but they help a lot with tasks in this chapter Basically an algorithm for this task is simple. The most difficult part here is to check if a word contains a number.
Regular expressions become quite helpful there (for checking if a string contains a number).
What you need to read about is String.matches() method and regular expressions (because .matches() take regex as argument).
But they didn't teach us about regular expressions yet, so you can use .contains() (and then think how not to write 10 ifs). But I recommend to take one or two hours off the codegym and learn something about regular expressions, and then you come here and make this task in 2 minutes;)
0
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.