Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- public class Files {
- public static void main(String[] args) {
- String data = "Program title: Balance\nAuthor: Tolkien\nCorporation: DMK\nPhone: +1-503-555-0091"
- + "\nDate Tues April 9, 2005\nVersion: 13.2\nLevel: Alpha";
- String program = "Wood";
- String version = "1.9";
- System.out.println(modifyData(data, program, version));
- }
- public static String modifyData(String data, String program, String version) {
- List<String> splitedRows = Arrays.stream(data.split("\\n")).toList();
- String correctProgram = parseProgram(splitedRows.get(0));
- String Author = "Author: g964";
- String phoneResult = parsePhone(splitedRows.get(3));
- String versionResult = parseVersion(splitedRows.get(5), version);
- if(phoneResult == null || versionResult == null) {
- return "ERROR: VERSION or PHONE";
- }
- return correctProgram + "\n" + Author + "\n" + "Phone: " + phoneResult + "\n" + "Date: 2019-01-01" + "\n" + versionResult;
- }
- private static String parseVersion(String stringVerion, String replacedVersion) {
- String[] splittedVersion = stringVerion.split(" ");
- String version = splittedVersion[1];
- String startString = "Version: ";
- if(version.matches("\\d+\\.\\d+")) {
- Double versionD = Double.valueOf(version);
- if(versionD != 2.0D) {
- return startString + replacedVersion;
- } else {
- return startString + "2.0";
- }
- } else {
- return null;
- }
- }
- private static String parsePhone(String phone) {
- String[] splitedPhoneLine = phone.split(" ");
- String splittedPhone = splitedPhoneLine[1];
- if(splittedPhone.matches("\\+1-(\\d{3})-(\\d{3})-(\\d{4})")) {
- return splittedPhone;
- }
- return null;
- }
- private static String parseProgram(String program) {
- String[] splittedProgram = program.split("( title: )");
- return splittedProgram[0] + ": " + splittedProgram[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement