Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can some convert this c++ code to Java for me??? // Call needed functions # include # include # include # include # include using

Can some convert this c++ code to Java for me???

// Call needed functions # include # include # include # include # include

using namespace std;

// Main function int main() { // Declare variables map NamSalMap; fstream name_cities; fstream name_salaries; ofstream data; string NameCity; string name; string sal; string user_input; string city; string person; string income; vector name_vector;

// Ask user for text file names and open text files data.open("data.txt"); name_cities.open("personnel_addresses2.txt"); name_salaries.open("personnel_salaries2.txt");

// Ask for user input and store in variable cout << "Please enter a city name or a partial city name:" << endl; cin >> user_input; // While loop to read through text file containing names and cities, name_cities. // Stores string in line while (getline(name_cities, NameCity)) { // Store just the city in string city, ignore name // That way when the program searches the file it will not match // with any names and just check for matches in the cities city = (NameCity.substr(NameCity.find("|") + 1, NameCity.back()));

// If statement to find if user input is in city if (city.find(user_input) != std::string::npos) { // Store name only in variable name name = NameCity.substr(0, NameCity.find("|")); // Insert name into vector name_vector.push_back(name); } }

// While loop to read through second text file containing names and salaries, salaries // Stores string in sal while (getline(name_salaries, sal)) { // Store name in variable person person = sal.substr(0, sal.find("|")); // Store salary in variable income income = (sal.substr(sal.find("|") + 1, sal.back())); // Insert both values into a map, entire file should be inserted into map NamSalMap[person] = income; }

// For loop to iterate through vector that contains the names of the people that lived in the cities that matched the user input for (vector::iterator t = name_vector.begin(); t != name_vector.end(); ++t) { // If statement to check annd see if name is in map, if it finds it enter statement if (NamSalMap.find(*t) != NamSalMap.end()) { // Set person equal to current value in vector person = *t; // Output the name of people who live in the cities matching the user input and their salaries separated by colon to text file data << person << ":" << NamSalMap[person] << endl; // Output the name of people who live in the cities matching the user input and their salaries separated by colon to shell cout << person << ":" << NamSalMap[person] << endl; } }

// Close all three files data.close(); name_cities.close(); name_salaries.close();

// End program return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions

Question

Use numerical integration to evaluate the following:

Answered: 1 week ago

Question

Conduct an effective performance feedback session. page 360

Answered: 1 week ago