JAVA Program ASAP Modify this program below so it is a Filesorting.java program so it passes all the test cases when I upload it to Hypergrade. I have provided the failed test cases a screenshot. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader(getFileName())); String line; while ((line = reader.readLine()) != null) { processAndPrintSortedLine(line); } reader.close(); } catch (IOException e) { System.out.println("Error reading the file: " + e.getMessage()); } } private static String getFileName() { String fileName; java.util.Scanner scanner = new java.util.Scanner(System.in); do { System.out.println("Please enter the file name or type QUIT to exit:"); fileName = scanner.nextLine(); if (fileName.equalsIgnoreCase("QUIT")) { System.exit(0); } if (!fileExists(fileName)) { System.out.println("File " + fileName + " is not found. Please re-enter the file name."); } } while (!fileExists(fileName)); return fileName; } private static boolean fileExists(String fileName) { try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { return true; } catch (IOException e) { return false; } } private static void processAndPrintSortedLine(String line) { String[] tokens = line.split(","); ArrayList numbers = new ArrayList<>(); for (String token : tokens) { numbers.add(Integer.parseInt(token)); } // Use any sorting method from Sort.java // For example: Sort.insertionSort(numbers.toArray(new Integer[0])); // Arrays.sort(numbers.toArray(new Integer[0])); Arrays.sort(numbers.toArray(new Integer[0])); // Using insertion sort for demonstration // Convert ArrayList to CharSequence[] CharSequence[] charSequences = new CharSequence[numbers.size()]; for (int i = 0; i < numbers.size(); i++) { charSequences[i] = numbers.get(i).toString(); } // Print the sorted numbers on the console System.out.println(String.join(",", charSequences)); } } input2x2.csv -67,-11 -27,-70 input1.csv 10 input10x10.csv 56,-19,-21,-51,45,96,-46 -27,29,-58,85,8,71,34 50,51,40,50,100,-82,-87 -47,-24,-27,-32,-25,46,88 -47,95,-41,-75,85,-16,43 -78,0,94,-77,-69,78,-25 -80,-31,-95,82,-86,-32,-22 68,-52,-4,-68,10,-14,-89 26,33,-59,-51,-48,-34,-52 -47,-24,80,16,80,-66,-42 input0.csv Test Case 1 Please enter the file name or type QUIT to exit:\n input1.csvENTER 10\n Test Case 2 Please enter the file name or type QUIT to exit:\n input2x2.csvENTER -67,-11\n -70,-27\n Test Case 3 Please enter the file name or type QUIT to exit:\n input10x10.csvENTER -51,-46,-21,-19,45,56,96\n -58,-27,8,29,34,71,85\n -87,-82,40,50,50,51,100\n -47,-32,-27,-25,-24,46,88\n -75,-47,-41,-16,43,85,95\n -78,-77,-69,-25,0,78,94\n -95,-86,-80,-32,-31,-22,82\n -89,-68,-52,-14,-4,10,68\n -59,-52,-51,-48,-34,26,33\n -66,-47,-42,-24,16,80,80\n Test Case 4 Please enter the file name or type QUIT to exit:\n input0.csvENTER File input0.csv is empty.\n Test Case 5 Please enter the file name or type QUIT to exit:\n input2.csvENTER File input2.csv is not found.\n Please re-enter the file name or type QUIT to exit:\n input1.csvENTER 10\n Test Case 6 Please enter the file name or type QUIT to exit:\n quitENTER Test Case 7 Please enter the file name or type QUIT to exit:\n input2.csvENTER File input2.csv is not found.\n Please re-enter the file name or type QUIT to exit:\n QUITENTER
JAVA
Modify this program below so it is a Filesorting.java program so it passes all the test cases when I upload it to Hypergrade. I have provided the failed test cases a screenshot.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader(getFileName()));
String line;
while ((line = reader.readLine()) != null) {
processAndPrintSortedLine(line);
}
reader.close();
} catch (IOException e) {
System.out.println("Error reading the file: " + e.getMessage());
}
}
private static String getFileName() {
String fileName;
java.util.Scanner scanner = new java.util.Scanner(System.in);
do {
System.out.println("Please enter the file name or type QUIT to exit:");
fileName = scanner.nextLine();
if (fileName.equalsIgnoreCase("QUIT")) {
System.exit(0);
}
if (!fileExists(fileName)) {
System.out.println("File " + fileName + " is not found. Please re-enter the file name.");
}
} while (!fileExists(fileName));
return fileName;
}
private static boolean fileExists(String fileName) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
return true;
} catch (IOException e) {
return false;
}
}
private static void processAndPrintSortedLine(String line) {
String[] tokens = line.split(",");
ArrayList<Integer> numbers = new ArrayList<>();
for (String token : tokens) {
numbers.add(Integer.parseInt(token));
}
// Use any sorting method from Sort.java
// For example: Sort.insertionSort(numbers.toArray(new Integer[0]));
// Arrays.sort(numbers.toArray(new Integer[0]));
Arrays.sort(numbers.toArray(new Integer[0])); // Using insertion sort for demonstration
// Convert ArrayList<Integer> to CharSequence[]
CharSequence[] charSequences = new CharSequence[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
charSequences[i] = numbers.get(i).toString();
}
// Print the sorted numbers on the console
System.out.println(String.join(",", charSequences));
}
}
input2x2.csv
-67,-11
-27,-70
input1.csv
10
input10x10.csv
56,-19,-21,-51,45,96,-46
-27,29,-58,85,8,71,34
50,51,40,50,100,-82,-87
-47,-24,-27,-32,-25,46,88
-47,95,-41,-75,85,-16,43
-78,0,94,-77,-69,78,-25
-80,-31,-95,82,-86,-32,-22
68,-52,-4,-68,10,-14,-89
26,33,-59,-51,-48,-34,-52
-47,-24,80,16,80,-66,-42
input0.csv
Test Case 1
input1.csvENTER
10\n
Test Case 2
input2x2.csvENTER
-67,-11\n
-70,-27\n
Test Case 3
input10x10.csvENTER
-51,-46,-21,-19,45,56,96\n
-58,-27,8,29,34,71,85\n
-87,-82,40,50,50,51,100\n
-47,-32,-27,-25,-24,46,88\n
-75,-47,-41,-16,43,85,95\n
-78,-77,-69,-25,0,78,94\n
-95,-86,-80,-32,-31,-22,82\n
-89,-68,-52,-14,-4,10,68\n
-59,-52,-51,-48,-34,26,33\n
-66,-47,-42,-24,16,80,80\n
Test Case 4
input0.csvENTER
File input0.csv is empty.\n
Test Case 5
input2.csvENTER
File input2.csv is not found.\n
Please re-enter the file name or type QUIT to exit:\n
input1.csvENTER
10\n
Test Case 6
quitENTER
Test Case 7
input2.csvENTER
File input2.csv is not found.\n
Please re-enter the file name or type QUIT to exit:\n
QUITENTER
Step by step
Solved in 4 steps with 12 images