d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > English To Spanish Dictionary Java Code > Please Help
Add Reply New Topic New Poll
Member
Posts: 10,543
Joined: May 4 2013
Gold: 7,938.50
Nov 2 2016 02:05pm
I don't even know where to begin. I can get as much fg as I need but need this done in an hour!



The English-Spanish dictionary:
Spanish Dictionary: An incomplete dictionary file (dict.txt) is provided for you to work with<---- All that is in this file is:: computer computadora, dog perro, queen reina, string cuerda, hello hola
So I just need a program that will


This dictionary lists a single English words followed by their corresponding Spanish translation. You need to implement a program, called SpanishDictionary that prompts the user to input a single English word (or “q” to quit) and then outputs the corresponding Spanish translation if the english appears in the dictionary file, dict.txt. Notice, your program must have a programloop that allows it to be repeated until the user enters a ‘q’ or ‘Q’ instead of an english word. The user indicating they wish to quit may not be in a separate input. It must be done from the same input where the user would enter the english word. Your code should be able to deal with the following cases:
1. If the input word is in the dictionary file, show both the English word and the corresponding Spanish word in a display including phrasing that shows their relation ( indicates the language of each word and the fact that one is the translation of the other). Quotes around the english word and spanish translation.
2. If the input words cannot be found, a message should indicate that the word can not be found. The program should then cycle.
3. If the input is q, then quit the loop and terminate the execution of the code with a termination message of your choice. Note: a word beginning with q, such as "queen" or “quaint”, should be considered valid.


This post was edited by Cold1992 on Nov 2 2016 02:06pm
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Nov 2 2016 03:01pm
So I assume this is java command line? I Think I could write this in ~ 5 minutes, but you did post this an hour ago. Still need it done?
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Nov 2 2016 03:18pm
I didn't have java installed so it took a minute extra.

Code
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.nio.file.Paths;
public class HelloWorld {

public static void main(String []args){
try {
Scanner input = new Scanner(System.in);
HashMap<String, String> dictionary = new HashMap<String, String>();
List<String> temp = Files.readAllLines(Paths.get("C:/temp", "dict.txt"),Charset.defaultCharset());
for(String s : temp) {
dictionary.put(s.split("\\s")[0], s.split("\\s")[1]);
}
System.out.println("Welcome to the English to Spanish dictionary.");
while (true) {
System.out.println("Enter an english word or q to quit: ");
String word = input.nextLine();
if(dictionary.containsKey(word)) {
System.out.printf("The english word '%s' translates to the spanish '%s'\n", word, dictionary.get(word) );
} else if (word.equals("q")) {
break;
} else {
System.out.println("That word was not in the dictionary.");
}
}
} catch (Exception e) {
e.printStackTrace();
}

}
}


Edit forgot the friggin main lmao.

Edit 2: Not sure if you HAD to read the thing from a file. It does now

This post was edited by waraholic on Nov 2 2016 03:40pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll