Results 1 to 2 of 2
Thread: Translator hashtable
- 01-10-2009, 09:10 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Translator hashtable
I've been working on a simple translator that replaces words in a short typed sentence. How do I make the hash table able to read an outside list so I don't have to add words one-by-one as I have been doing so far?
Here's the code:
package compSci;
import java.util.*;
public class Translator {
public static void main(String[] args) {
String sentence;
String translatedSentence;
String word1 = "";
String word2 = "";
String word3 = "";
String word4 = "";
String tWord1 = "";
String tWord2 = "";
String tWord3 = "";
String tWord4 = "";
Hashtable list = new Hashtable();
list.put("you", "tu");
list.put("are", "estas");
list.put("how", "como");
list.put ("hello", "hola");
sentence = Keyboard.readString();
StringTokenizer translate = new StringTokenizer(sentence);
if (translate.countTokens() == 3)
{
word1 = translate.nextToken();
word2 = translate.nextToken();
word3 = translate.nextToken();
}
if (translate.countTokens() == 4)
{
word1 = translate.nextToken();
word2 = translate.nextToken();
word3 = translate.nextToken();
word4 = translate.nextToken();
}
tWord1 = (String) list.get(word1);
tWord2 = (String) list.get(word2);
tWord3 = (String) list.get(word3);
tWord4 = (String) list.get(word4);
translatedSentence = tWord1 + " " + tWord2 + " " + tWord3 + " " + tWord4 + ".";
System.out.println(translatedSentence);
}
}
- 01-11-2009, 02:02 AM #2
I assume this is a school assignment. Put all the hashTable.put() statements in method like initHashTable(). Make one statement, copy it, then paste it a bunch of times. You have to type your words somewhere, so you might as well type it there.
Normally, that sort of information would be stored in a database, and there would be a maintenance application to allow pairs to be added or modified.
Similar Threads
-
Custom Font Translator
By Nerdopolis in forum New To JavaReplies: 3Last Post: 04-18-2009, 03:50 AM -
Hashtable
By angelicsign in forum New To JavaReplies: 6Last Post: 02-05-2009, 04:30 PM -
Java multilanguage translator
By mnprakash in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-05-2009, 08:13 AM -
Multidimensional hashtable?
By jklsemicolon in forum New To JavaReplies: 6Last Post: 08-17-2008, 05:23 AM -
Hashtable example
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks