JAVA Program ASAP Combine these two program into one so it will the passes the test cases. Converttext.java import java.io.*; import java.util.Scanner; public class ConvertText { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("Please enter the file name or type QUIT to exit:"); while (true) { String input = sc.next(); if (input.equalsIgnoreCase("QUIT")) { break; // Exit the program } else { String filePath = new File("").getAbsolutePath() + "/" + input; File file = new File(filePath); if (file.exists() && !file.isDirectory()) { try (BufferedReader br = new BufferedReader(new FileReader(file))) { String st; StringBuilder formattedText = new StringBuilder(); while ((st = br.readLine()) != null) { // Split the input line into sentences using space as the delimiter String[] sentences = st.split(" "); for (int i = 0; i < sentences.length; i++) { StringBuilder sb = new StringBuilder(""); sb.append(Character.toUpperCase(sentences[i].charAt(0))); for (int j = 1; j < sentences[i].length(); j++) { char currentChar = sentences[i].charAt(j); if (currentChar == '!' || currentChar == '.') { sb.append(currentChar); if (j + 1 < sentences[i].length()) { sb.append(" "); sb.append(Character.toUpperCase(sentences[i].charAt(j + 1))); j++; } } else if (Character.isUpperCase(currentChar)) { sb.append(" "); sb.append(Character.toLowerCase(currentChar)); } else { sb.append(currentChar); } } formattedText.append(sb.toString() + ""); } formattedText.append("\n"); } System.out.print(formattedText); return; // Exit the program after printing } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("File '" + input + "' is not found."); System.out.println("Please re-enter the file name or type QUIT to exit:"); } } } } } FileSorting.java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class FileSorting { private static final String FILENAME = "somefile.txt"; public static void main(String[] args) { // read integers from input file Scanner fileReader; ArrayList numbers = new ArrayList<>(); try { fileReader = new Scanner(new File(FILENAME)); while(fileReader.hasNextLine()) { numbers.add(Integer.parseInt(fileReader.nextLine().trim())); } fileReader.close(); }catch(FileNotFoundException fnfe){ System.out.println("File " + FILENAME + " is not found."); } // check whether the file is empty or not if(numbers.isEmpty()) { System.out.println("File " + FILENAME + " is empty."); return; } System.out.println("ORIGINAL LIST: " + numbers); // sort the arraylist sort(numbers); // print the arraylist System.out.println("SORTED LIST: " + numbers); } // this method takes an arraylist of integers as parameter and sorts the list in ascending order private static void sort(ArrayList nums) { int i, j, temp; for(i = 0; i < (nums.size() - 1); i++) { for(j = 0; j < (nums.size() - i - 1); j++) { if(nums.get(j) > nums.get(j + 1)) { temp = nums.get(j); nums.set(j, nums.get(j + 1)); nums.set(j + 1, temp); } } } } }
JAVA Program ASAP
Combine these two program into one so it will the passes the test cases.
Converttext.java
import java.io.*;
import java.util.Scanner;
public class ConvertText {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the file name or type QUIT to exit:");
while (true) {
String input = sc.next();
if (input.equalsIgnoreCase("QUIT")) {
break; // Exit the program
} else {
String filePath = new File("").getAbsolutePath() + "/" + input;
File file = new File(filePath);
if (file.exists() && !file.isDirectory()) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String st;
StringBuilder formattedText = new StringBuilder();
while ((st = br.readLine()) != null) {
// Split the input line into sentences using space as the delimiter
String[] sentences = st.split(" ");
for (int i = 0; i < sentences.length; i++) {
StringBuilder sb = new StringBuilder("");
sb.append(Character.toUpperCase(sentences[i].charAt(0)));
for (int j = 1; j < sentences[i].length(); j++) {
char currentChar = sentences[i].charAt(j);
if (currentChar == '!' || currentChar == '.') {
sb.append(currentChar);
if (j + 1 < sentences[i].length()) {
sb.append(" ");
sb.append(Character.toUpperCase(sentences[i].charAt(j + 1)));
j++;
}
} else if (Character.isUpperCase(currentChar)) {
sb.append(" ");
sb.append(Character.toLowerCase(currentChar));
} else {
sb.append(currentChar);
}
}
formattedText.append(sb.toString() + "");
}
formattedText.append("\n");
}
System.out.print(formattedText);
return; // Exit the program after printing
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("File '" + input + "' is not found.");
System.out.println("Please re-enter the file name or type QUIT to exit:");
}
}
}
}
}
FileSorting.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileSorting {
private static final String FILENAME = "somefile.txt";
public static void main(String[] args) {
// read integers from input file
Scanner fileReader;
ArrayList<Integer> numbers = new ArrayList<>();
try
{
fileReader = new Scanner(new File(FILENAME));
while(fileReader.hasNextLine())
{
numbers.add(Integer.parseInt(fileReader.nextLine().trim()));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("File " + FILENAME + " is not found.");
}
// check whether the file is empty or not
if(numbers.isEmpty())
{
System.out.println("File " + FILENAME + " is empty.");
return;
}
System.out.println("ORIGINAL LIST: " + numbers);
// sort the arraylist
sort(numbers);
// print the arraylist
System.out.println("SORTED LIST: " + numbers);
}
// this method takes an arraylist of integers as parameter and sorts the list in ascending order
private static void sort(ArrayList<Integer> nums)
{
int i, j, temp;
for(i = 0; i < (nums.size() - 1); i++)
{
for(j = 0; j < (nums.size() - i - 1); j++)
{
if(nums.get(j) > nums.get(j + 1))
{
temp = nums.get(j);
nums.set(j, nums.get(j + 1));
nums.set(j + 1, temp);
}
}
}
}
}
Step by step
Solved in 4 steps with 3 images