Java Program ASAP   ************This program must work in hypergrade and pass all the test cases.**********   Here is a working program. Please modidy this program so it passes the test cases down below. I have provided the correct test case as a screenshot.    For Test Case 1 first print out Please enter the file name or type QUIT to exit:\ then you type text1.txtENTER and it displays Stop And Smell The Roses./n there needs to be nothing after that.   For test case 2 first print out Please enter the file name or type QUIT to exit: then you type txt1.txtENTER then it reads out File 'txt1.txt' is not found.\n  Then it didplays Please re-enter the file name or type QUIT to exit:\n after the test file is not found. then you type in text1.txt and it displays stop and smell the roses.\n.   For test case 3 first print out Please enter the file name or type QUIT to exit: then you type text2.txtENTER and it displays A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n and there needs to be nothing after that.    For test case 4 first print out Please enter the file name or type QUIT to exit: then you type somefile.txtENTER and it displays File 'somefile.txt' is not found.\n then it displays Please re-enter the file name or type QUIT to exit:\n then you type anotherbadfile.txtENTER and it dispalys File 'anotherbadfile.txt' is not found.\n then in. repeats Please re-enter the file name or type QUIT to exit:\n and you type quit and it ends the program.    import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class MorseCodeConverter {     public static void main(String[] args) {         Map morseCodeMap = readMorseCodeTable("morse.txt");         Scanner scanner = new Scanner(System.in);             System.out.print("Please enter the file name or type QUIT to exit:\n");             do{             String fileName = scanner.nextLine().trim();             if (fileName.equalsIgnoreCase("QUIT")) {                 break;             }             try {                 String text = convertMorseCodeToText(fileName, morseCodeMap);                 System.out.println(text);                 break;             } catch (IOException e) {                 System.out.println("File '" + fileName + "' is not found.");                 System.out.print("Please re-enter the file name or type QUIT to exit:\n");                         }         }while(true);         scanner.close();     }     private static Map readMorseCodeTable(String fileName) {         Map morseCodeMap = new HashMap<>();         try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {             String line;             while ((line = reader.readLine()) != null) {                 String[] parts = line.split("\\s+");                 if (parts.length == 2) {                     morseCodeMap.put(parts[1], parts[0]);                 }             }         } catch (IOException e) {             e.printStackTrace();         }         return morseCodeMap;     }     private static String convertMorseCodeToText(String fileName, Map morseCodeMap) throws IOException {         StringBuilder result = new StringBuilder();         try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {             String line;             while ((line = reader.readLine()) != null) {                 String[] morseWords = line.split("\\s{3,}");                 for (String morseWord : morseWords) {                     String[] morseLetters = morseWord.split("\\s+");                     for (String morseLetter : morseLetters) {                         if (morseCodeMap.containsKey(morseLetter)) {                             result.append(morseCodeMap.get(morseLetter));                         }                     }                                    }                 result.append("\n");             }         }         return result.toString().trim();     } }                             text1.txt StopAndSmellTheRoses.   text2.txt ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameS

Programming with Microsoft Visual Basic 2017
8th Edition
ISBN:9781337102124
Author:Diane Zak
Publisher:Diane Zak
Chapter7: String Manipulation
Section: Chapter Questions
Problem 6E
icon
Related questions
Question
Java Program ASAP
 
************This program must work in hypergrade and pass all the test cases.**********
 
Here is a working program. Please modidy this program so it passes the test cases down below. I have provided the correct test case as a screenshot. 
 
For Test Case 1 first print out Please enter the file name or type QUIT to exit:\ then you type text1.txtENTER and it displays Stop And Smell The Roses./n there needs to be nothing after that.
 
For test case 2 first print out Please enter the file name or type QUIT to exit: then you type txt1.txtENTER then it reads out File 'txt1.txt' is not found.\n  Then it didplays Please re-enter the file name or type QUIT to exit:\n after the test file is not found. then you type in text1.txt and it displays stop and smell the roses.\n.
 
For test case 3 first print out Please enter the file name or type QUIT to exit: then you type text2.txtENTER and it displays A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n and there needs to be nothing after that. 
 
For test case 4 first print out Please enter the file name or type QUIT to exit: then you type somefile.txtENTER and it displays File 'somefile.txt' is not found.\n then it displays Please re-enter the file name or type QUIT to exit:\n then you type anotherbadfile.txtENTER and it dispalys File 'anotherbadfile.txt' is not found.\n then in. repeats Please re-enter the file name or type QUIT to exit:\n and you type quit and it ends the program. 
 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MorseCodeConverter {

    public static void main(String[] args) {
        Map<String, String> morseCodeMap = readMorseCodeTable("morse.txt");

        Scanner scanner = new Scanner(System.in);
            System.out.print("Please enter the file name or type QUIT to exit:\n");
            do{
            String fileName = scanner.nextLine().trim();
            if (fileName.equalsIgnoreCase("QUIT")) {
                break;
            }

            try {
                String text = convertMorseCodeToText(fileName, morseCodeMap);
                System.out.println(text);
                break;
            } catch (IOException e) {
                System.out.println("File '" + fileName + "' is not found.");
                System.out.print("Please re-enter the file name or type QUIT to exit:\n");
           
            }
        }while(true);

        scanner.close();
    }

    private static Map<String, String> readMorseCodeTable(String fileName) {
        Map<String, String> morseCodeMap = new HashMap<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length == 2) {
                    morseCodeMap.put(parts[1], parts[0]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return morseCodeMap;
    }

    private static String convertMorseCodeToText(String fileName, Map<String, String> morseCodeMap) throws IOException {
        StringBuilder result = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] morseWords = line.split("\\s{3,}");
                for (String morseWord : morseWords) {
                    String[] morseLetters = morseWord.split("\\s+");
                    for (String morseLetter : morseLetters) {
                        if (morseCodeMap.containsKey(morseLetter)) {
                            result.append(morseCodeMap.get(morseLetter));
                        }
                    }
                  
                }
                result.append("\n");
            }
        }
        return result.toString().trim();
    }
}

 
 
 
 
 
 
 
 
 
 
 

 

 
 
text1.txt
StopAndSmellTheRoses.
 
text2.txt
ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit.
PleaseExplainABitMoreInTheWayOfFootnotes.FromTheGivenTextIt'sNotClearWhatAreWeReadingAbout.
Test Case 1
Please enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop and smell the roses. \n
Test Case 2
Please enter the file name or type QUIT to exit: \n
txt1.txt ENTER
File 'txt1.txt' is not found. \n
Please re-enter the file name or type QUIT to exit: \n
text1.txt ENTER
Stop and smell the roses.\n
Test Case 3
Please enter the file name or type QUIT to exit: \n
text2.txt ENTER
A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n
Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n
Test Case 4
Please enter the file name or type QUIT to exit: \n
somefile.txt ENTER
File 'somefile.txt' is not found.\n
Please re-enter the file name or type QUIT to exit: \n
anotherbadfile.txt ENTER
File 'anotherbadfile.txt' is not found. \n
Please re-enter the file name or type QUIT to exit: \n
quit ENTER
Transcribed Image Text:Test Case 1 Please enter the file name or type QUIT to exit: \n text1.txt ENTER Stop and smell the roses. \n Test Case 2 Please enter the file name or type QUIT to exit: \n txt1.txt ENTER File 'txt1.txt' is not found. \n Please re-enter the file name or type QUIT to exit: \n text1.txt ENTER Stop and smell the roses.\n Test Case 3 Please enter the file name or type QUIT to exit: \n text2.txt ENTER A true rebel you are! Everyone was impressed. You'll do well to continue in the same spirit.\n Please explain a bit more in the way of footnotes. From the given text it's not clear what are we reading about.\n Test Case 4 Please enter the file name or type QUIT to exit: \n somefile.txt ENTER File 'somefile.txt' is not found.\n Please re-enter the file name or type QUIT to exit: \n anotherbadfile.txt ENTER File 'anotherbadfile.txt' is not found. \n Please re-enter the file name or type QUIT to exit: \n quit ENTER
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L