Question

Modification of original code so it can include 1) at least one array 2)*New English words...

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\nEnter 2 to search word\nEnter 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() + "\n" +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()+"\n");
}
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).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implementation in java using a 2d array as per the requirement and performing all the read and write operations to a text file. Also, the words in the dictionary are stored in sorted/ lexicographical order


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


//*SECOND CLASS


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*


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;
}
}

public class Translation {
public static void main(String[] args) throws IOException {
  

// the size of the array can be any large number/
String a[][] = new int[10000][2]; // creating an array as per the requirement which will be used to store and sort the set of // English- Spanish pairs in the text file.
  
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);
int cnt = 0; // to store the number of rows in a dictionary
  
while (reader.hasNextLine()) {
line = reader.nextLine();
FlashCard fcard=new FlashCard(line.split(" ")[0],line.split(" ")[1]);
a[cnt][0] = line.split(" ")[0]; // populating the array with english word
a[cnt][1] = line.split(" ")[1]; // populating the array with spanish word
cnt++; // incrementing the count
}
  
System.out.println("Data loaded successfully");
reader.close();
EnglishOptions eo=new EnglishOptions();
while(choice!=3){
System.out.println("Enter 1 to add word\nEnter 2 to search word\nEnter 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);
a[cnt][0] = english; // adding 1 more english word in the array
a[cnt][1] = spanish; // adding 1 more spanish words in the array
  
System.out.println(eo.getsuccessMessage());
}else if(choice==2){
System.out.println(eo.getEnToEs() + "\n" +eo.getEsToEn());
int lang = in.nextInt();
System.out.println(eo.getWordPrompt());

String wordSearch = inString.nextLine();
if(lang==1){
for(int i = 0;i<cnt;i++)  
{
if(a[i][0].equals(wordSearch))
{
System.out.println(wordSearch +" in Spanish is "+a[i][1]);
flag = 1   
break;
}
  
}
if(flag == 0)
{
System.out.println(wordSearch+" not found!");
  
}

}
else if(lang==2){

boolean found= false;
String result="";

for(int i = 0;i<cnt;i++)
{
if(a[i][1].equals(wordSearch))
{
System.out.println(wordSearch +" in English is "+a[i][0]);
found = true;
break;
}
  
}
if(!found)
{
System.out.println(wordSearch+" not found!");
}

  

if(found){
System.out.println(wordSearch +" in English is "+result);
}else{
System.out.println(wordSearch+" not found!");
}
}
} else if(choice==3){
  
// overriding the compare method of Comparator interface.
Arrays.sort(a, new Comparator<String[]>(){
@Override
public int compare(String[] s1, String[] entry2) {
String e1 = s1[0]; // getting the english word from one row
String e2 = s1[0]; // getting the english word from another row
  
return e1.compareToIgnoreCase(e2); // this method return positive number if e1 > e2 ,
// return negative if e1 < e2, all the comparison is done lexicographically
}   
  
});
  

FileWriter out =new FileWriter("Dictionary.txt");
  
for(String[] engl_span : a)
{
out.write(engl_span[0]+ " "+engl_span[1] +"\n");
}
out.close();
  
out.close();
}
}
in.close();
inString.close();
}
}

Add a comment
Know the answer?
Add Answer to:
Modification of original code so it can include 1) at least one array 2)*New English words...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT