Question: Modification of original code so it can include 1) at least one array 2)*New English words that are written/saved to the .txt file(s) must be

Modification of original code so it can include 1) at least one array 2)*New English words that are written/saved to the .txt file(s) must be in alphabetical order* (e.g, Apple cannot be in a lower line than Orange). This is what I'm really struggling with as I don't know how to organize alphabetically only the english word translation without the spanish equivalent word also getting organized.

The only idea I have right now is to recreate the code using two dimensional arrays, but I don't if that would fulfill the purpose

Further explanation of the code at the end if needed.

//*Main Class*

package translation; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner;

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("Dictionary.txt"); Scanner reader = new Scanner(file); while (reader.hasNextLine()) { line = reader.nextLine(); FlashCard fcard=new FlashCard(line.split(" ")[0],line.split(" ")[1]); dictionary.put(line.split(" ")[0], fcard); } System.out.println("Data loaded successfully"); reader.close(); EnglishOptions eo=new EnglishOptions(); 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(eo.getEnterEnWord()); english=inString.nextLine(); System.out.println(eo.getEnterEsWord()); spanish=inString.nextLine(); FlashCard card=new FlashCard(english,spanish); dictionary.put(english,card); System.out.println(eo.getsuccessMessage()); }else if(choice==2){ System.out.println(eo.getEnToEs() + " " +eo.getEsToEn()); int lang = in.nextInt(); System.out.println(eo.getWordPrompt()); String wordSearch = inString.nextLine(); if(lang==1){ if(dictionary.containsKey(wordSearch)){ System.out.println(wordSearch +" in Spanish is "+dictionary.get(wordSearch).getSpanishWord()); }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().getSpanishWord().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("Dictionary.txt"); for (Map.Entry entry : dictionary.entrySet()) { out.write(entry.getKey()+" "+entry.getValue().getSpanishWord()+" "); } out.close(); } } in.close(); inString.close(); } }

//*SECOND CLASS

package translation; public class FlashCard { private String englishWord; private String spanishWord;

//arg constructor public FlashCard(String enWord,String esWord) { this.englishWord=enWord; this.spanishWord=esWord; }

/** * @return the englishWord */ public String getEnglishWord() { return englishWord; }

/** * @param englishWord the englishWord to set */ public void setEnglishWord(String englishWord) { this.englishWord = englishWord; }

/** * @return the spanishWord */ public String getSpanishWord() { return spanishWord; }

/** * @param spanishWord the spanishWord to set */ public void setSpanishWord(String spanishWord) { this.spanishWord = spanishWord; }

}

//*THIRD CLASS*

package translation; public class EnglishOptions { private String enToEs; private String esToEn; private String enterEnWord; private String enterEsWord; private String successMessage; private String wordPrompt; public EnglishOptions() { this.enterEnWord="Enter english word"; this.enterEsWord="Enter spanish word"; this.enToEs="Enter 1 for English to Spanish"; this.esToEn="Enter 2 for Spanish to English"; this.successMessage="Flash Card added sucessfully!"; this.wordPrompt="Enter the word"; } /** * @return the enterEnWord */ public String getEnterEnWord() { return enterEnWord; } /** * @return the enterEsWord */ public String getEnterEsWord() { return enterEsWord; }

/** * @return the enToEs */ public String getEnToEs() { return enToEs; }

/** * @return the esToEn */ public String getEsToEn() { return esToEn; } public String getsuccessMessage() { return successMessage; }

public String getWordPrompt() { return wordPrompt; } }

The main goal of the code is to create an expandable custom 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).

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!