Results 1 to 2 of 2
  1. #1
    editor35 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

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

    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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

  1. Custom Font Translator
    By Nerdopolis in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 04:50 AM
  2. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:30 PM
  3. Java multilanguage translator
    By mnprakash in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-05-2009, 09:13 AM
  4. Multidimensional hashtable?
    By jklsemicolon in forum New To Java
    Replies: 6
    Last Post: 08-17-2008, 06:23 AM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •