Question: Java coding assignment custom expandable translator- When program starts 1) ask user if they want to add or look up a word Add a word

Java coding assignment custom expandable translator- When program starts 1) ask user if they want to add or look up a word

Add a word option: 1) ask for English word version 2) Then ask for other language word version 3) *New words that are written to the english .txt file must be in alphabetical order* (e.g, Apple cannot be in a lower line than Orange) 4) Any method to store data in file (e.g., one data.txt file or more)

Look up a word option: 1) ask which language the user wants to start with 2) Ask for word entry 3) Program should find the equivalent word in the other language

Code requirements: At least one array, use of user input, save or read files from a text file(s) , a loop, an conditional statement, exception handling, *Appropriate object classes that encapsulate some program logic* (User Defined Object-Oriented Solution appropriate object classes that encapsulate some program logic)

The following code works and fulfills the assignment, however apparently my instructor argues that it dosen't complete all the requirements.Bold indicates what my instructor says is missing or I couldn't manage to do.

package translation; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Translation { public static void main(String[] args) throws IOException { Map dictionary = new HashMap<>(); String english,spanish; int choice=0; Scanner in = new Scanner(System.in); Scanner inString = new Scanner(System.in); String line; File file = new File("Output.txt"); Scanner reader = new Scanner(file); while (reader.hasNextLine()) { line = reader.nextLine(); dictionary.put(line.split(" ")[0], line.split(" ")[1]); } System.out.println("Data loaded successfully"); reader.close(); while(choice!=3){ System.out.println("Enter 1 to add word Enter 2 to search word Enter 3 to store data and quit"); choice = in.nextInt(); if(choice==1){ System.out.println("Enter english word"); english=inString.nextLine(); System.out.println("Enter spanish word"); spanish=inString.nextLine(); dictionary.put(english,spanish); System.out.println("Word added"); }else if(choice==2){ System.out.println("Enter 1 for English to Spanish Enter 2 for Spanish to English"); int lang = in.nextInt(); System.out.println("Enter the word"); String wordSearch = inString.nextLine(); if(lang==1){ if(dictionary.containsKey(wordSearch)){ System.out.println(wordSearch +" in Spanish is "+dictionary.get(wordSearch)); }else{ System.out.println(wordSearch+" not found!"); } }else if(lang==2){ boolean found= false; String result=""; for (Map.Entry entry : dictionary.entrySet()) { if(entry.getValue().equalsIgnoreCase(wordSearch)){ found = true; result = entry.getKey(); } } if(found){ System.out.println(wordSearch +" in English is "+result); }else{ System.out.println(wordSearch+" not found!"); } } } else if(choice==3){ FileWriter out =new FileWriter("Output.txt"); for (Map.Entry entry : dictionary.entrySet()) { out.write(entry.getKey()+" "+entry.getValue()+" "); } out.close(); } } in.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!