Created
November 10, 2014 12:34
-
-
Save OlgaKulikova/81eaaf0632ea5e500db1 to your computer and use it in GitHub Desktop.
Translator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translator; | |
// Написать программу переводчик, которая будет переводить текст, | |
// написанный на одном языке, на другой язык согласно заранее составленному словарю. | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Vocabulary vocab = new Vocabulary(); | |
vocab.addToVocabulary("мама", "mother"); | |
vocab.addToVocabulary("мыла", "washed"); | |
vocab.addToVocabulary("раму", "frame"); | |
Scanner scan = new Scanner(System.in); | |
String text = scan.nextLine(); | |
System.out.println(vocab.translator(text)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package translator; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Vocabulary { | |
private static Map<String, String> voc = new HashMap<>(); | |
public void addToVocabulary(String eng, String rus) { | |
voc.put(eng, rus); | |
} | |
public String translator(String text) { | |
String[] words = text.split(" "); | |
StringBuilder sb = new StringBuilder(); | |
for (String t : words) { | |
sb.append(voc.get(t) + " "); // Вот здесь что-то не то выходит, в строку не попадает значение, а только null | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment