Question: hello can anyone convert this java code to c++ code import java.io.BufferedReader; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.io.IOException; public class Index { public

hello can anyone convert this java code to c++ code

import java.io.BufferedReader; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.io.IOException;

public class Index { public static HashMap buildIndex(String indexFileName, String addressFileName) { try { // open the files Scanner iScanner = new Scanner(new File(indexFileName)); Scanner aScanner = new Scanner(new File(addressFileName));

// Initialise a hashmap HashMap map = new HashMap(); // iterate the files until there are lines in both while(iScanner.hasNextLine() && aScanner.hasNextLine()) { // word from index.txt is the key for the HashMap // put the key value pair map.put(iScanner.nextLine(), aScanner.nextLine()); }

// close the FIle iScanner.close(); aScanner.close();

// return map return map; } catch (IOException e) { // unable to open file System.err.println("Unable to open file: " + e); // retuen an empty hashmap return new HashMap(); } }

// main method public static void main(String[] args) { // create the HashMap HashMap indexMap = buildIndex("index.txt", "address.txt"); // Ask for key from user Scanner scn = new Scanner(System.in);

System.out.println("Enter index: "); String index = scn.next();

// check if index exists if (indexMap.containsKey(index)) { // print System.out.println(index + " refers to " + indexMap.get(index)); } else { // not found System.out.println(index + " not found");

} // close scanner scn.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!