Do you need to store the words in a text file (i.e., do they need to persist), or can you store them in memory?
If they need to be written to a text file, try this:
// Create a file
File file = new File("file.txt");
// Initialize a print writer to print to the file
PrintWriter pw = new PrintWriter(file);
Scanner keyboard = new Scanner(System.in);
// Populate
boolean stop = false;
do {
String word;
String translation;
System.out.print("Enter a word: ");
word = keyboard.nextLine().trim() + " ";
if (!word.equals("quit ")) {
pw.print(word);
System.out.print("Enter its translation: ");
translation = keyboard.nextLine().trim();
pw.println(translation);
} else {
stop = true;
}
} while (!stop);
// Close the print writer and write to the file
pw.close();
// Initialize a scanner to read the file
Scanner fileReader = new Scanner(file);
// Initialize a hash table to store the values from the file
Hashtable<String, String> words = new Hashtable<String, String>();
// Add the information from the file to the hash table
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
String[] array = line.split(" ");
words.put(array[0], array[1]);
}
// Print the results
System.out.println("Results: ");
words.forEach((k, v) -> System.out.println(k + " " + v));
fileReader.close();
keyboard.close();
Note that I am using a space to separate the word from its translation. You can just as easily use a comma or a semicolon or what have you. Just replace line.split(" ") with line.split(< your separating character here>) and concatenate it to the end of word = keyboard.nextLine().trim().
If you don't need to save the information and just need to collect the user's input, it's even simpler:
Scanner keyboard = new Scanner(System.in);
// Initialize a hash table to store the values from the user
Hashtable<String, String> words = new Hashtable<String, String>();
// Get the input from the user
boolean stop = false;
do {
String word;
String translation;
System.out.print("Enter a word: ");
word = keyboard.nextLine().trim();
if (!word.equals("quit")) {
System.out.print("Enter its translation: ");
translation = keyboard.nextLine().trim();
words.put(word, translation);
} else {
stop = true;
}
} while (!stop);
// Print the results
System.out.println("Results: ");
words.forEach((k, v) -> System.out.println(k + " " + v));
keyboard.close();