Java Program ASAP Please modify this program so it passes all the test cases in Hypergrade. The output is not displaying the commas after the numbers and for the test cases 1-4 there needs to be nothing displayed after that and for test case 5 and 7 after Please re-enter the file name or type QUIT to exit: there needs to be nothing displayed after that. Here are the input files: 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 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; public class FileSorting { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Please enter the file name or type QUIT to exit:"); String fileName = scanner.nextLine(); if (fileName.equalsIgnoreCase("QUIT")) { break; } try { ArrayList lines = readFile(fileName); if (lines.isEmpty()) { System.out.println("File " + fileName + " is empty."); } else { performSorting(lines); } } catch (IOException e) { System.out.println("File " + fileName + " is not found."); if (!fileName.equalsIgnoreCase("QUIT")) { System.out.println("Please re-enter the file name or type QUIT to exit:"); } } } scanner.close(); } private static ArrayList readFile(String fileName) throws IOException { ArrayList lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } return lines; } private static void performSorting(ArrayList lines) { try { ArrayList> numbers = new ArrayList<>(); for (String line : lines) { String[] elements = line.split(","); ArrayList row = new ArrayList<>(); for (String element : elements) { row.add(Integer.parseInt(element)); } numbers.add(row); } for (ArrayList row : numbers) { Integer[] array = row.toArray(new Integer[0]); Arrays.sort(array); // Use Arrays.sort() for sorting System.out.println(Arrays.toString(array).replaceAll("[\\[\\],]", "")); } } catch (NumberFormatException | InputMismatchException e) { System.out.println("Invalid file format."); } } }
Java Program ASAP
Please modify this program so it passes all the test cases in Hypergrade. The output is not displaying the commas after the numbers and for the test cases 1-4 there needs to be nothing displayed after that and for test case 5 and 7 after Please re-enter the file name or type QUIT to exit: there needs to be nothing displayed after that.
Here are the input files:
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FileSorting {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please enter the file name or type QUIT to exit:");
String fileName = scanner.nextLine();
if (fileName.equalsIgnoreCase("QUIT")) {
break;
}
try {
ArrayList<String> lines = readFile(fileName);
if (lines.isEmpty()) {
System.out.println("File " + fileName + " is empty.");
} else {
performSorting(lines);
}
} catch (IOException e) {
System.out.println("File " + fileName + " is not found.");
if (!fileName.equalsIgnoreCase("QUIT")) {
System.out.println("Please re-enter the file name or type QUIT to exit:");
}
}
}
scanner.close();
}
private static ArrayList<String> readFile(String fileName) throws IOException {
ArrayList<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
}
return lines;
}
private static void performSorting(ArrayList<String> lines) {
try {
ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();
for (String line : lines) {
String[] elements = line.split(",");
ArrayList<Integer> row = new ArrayList<>();
for (String element : elements) {
row.add(Integer.parseInt(element));
}
numbers.add(row);
}
for (ArrayList<Integer> row : numbers) {
Integer[] array = row.toArray(new Integer[0]);
Arrays.sort(array); // Use Arrays.sort() for sorting
System.out.println(Arrays.toString(array).replaceAll("[\\[\\],]", ""));
}
} catch (NumberFormatException | InputMismatchException e) {
System.out.println("Invalid file format.");
}
}
}
Step by step
Solved in 4 steps with 3 images