Write a program that accepts an array of strings and a HashMap that represents a dictionary. The program must replace each element in the array with the corresponding value from the dictionary, if it exists. If an element doesn't exist in the dictionary, it must be left unchanged. The program must print out the modified array.
Introduction to HashMap
Replacing words
Report a typo
Sample Input 1:
5
dog cat bird dog bird
2
dog canine cat felineSample Output 1:
canine feline bird canine birdWrite a program in Java 17
import java.util.HashMap;
import java.util.Scanner;
class Main {
private static void printReplacedString( HashMap<String,String> map, String[] array) {
// implement me
}
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] array = new String [n];
for (int i = 0; i < n; ++i) {
array[i] = scanner.next();
}
int m = scanner.nextInt();
for (int i = 0; i < m; ++i) {
map.put(scanner.next(), scanner.next());
}
printReplacedString(map,array);
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.