Help needed. My code works but not validating. Checked other question but cant find answers there. I think the issue has to do with the second requirement
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int bytesRead = 0;
ArrayList<File> list = new ArrayList<>();
String s;
//read file names and get ouputstream filename
while(true) {
switch (s = reader.readLine()) {
}
if (s.equals("end")) break;
list.add(new File(s));
}
reader.close();
// Get filename to write to
String pathName = list.get(0).getAbsolutePath();
String filename = pathName.substring(pathName.indexOf(pathName.charAt(0)), pathName.indexOf("part")-1);
FileOutputStream fout = new FileOutputStream(filename);
//sort files according to partN
Collections.sort(list, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
String s1 = o1.getName();
String s2 = o2.getName();
int n1 = Integer.parseInt(s1.substring(s1.indexOf("part") + 4, s1.length()));
int n2 = Integer.parseInt(s2.substring(s2.indexOf("part") + 4, s2.length()));
if (n1 > n2) {
return 1;
} else if (n1 < n2) {
return -1;
}
return 0;
}
});
//write files
for(File file : list){
FileInputStream fin = new FileInputStream(file);
byte[] buffer = new byte[fin.available()];
fin.read(buffer);
fout.write(buffer);
fin.close();
}
fout.close();
}
}