A java code to search for a given keyword, e.g., robot, fitness, wearable and the result should
Question:
A java code to search for a given keyword, e.g., "robot", "fitness", "wearable" and the result
should present "fund_raised_percent" and "close_date". (Note: I have wrote the code for this but struggling after this)
? The search should include a memory. The memory means that it keeps the track of what has been searched. In simple words, you need tostoreinputs which we give to the search.
? This means the search criteria and timestamp, should be stored in acollection.
? There should be a method to call and print the number of search terms, their timestamps,
and frequency.
Code for keyword searched is following:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class TestSearch {
class Pair {
String clickthrough_url;
String close_date;
String funds_raised_percent;
public Pair(String url, String closeDate, String fundsRP) {
this.clickthrough_url = url;
this.close_date = closeDate;
this.funds_raised_percent = fundsRP;
}
}
ArrayList data = new ArrayList<>(); // Stores the data of file
// Reads the data from file to data List
void readData(String fileName) throws FileNotFoundException, IOException {
try (BufferedReader br = new BufferedReader(new FileReader("file3.csv"))) {
// Reading the next line
String line = br.readLine();
// If next line is not null
while (line != null) {
// Getting the columns and adding it to data List
String[] columns = line.split(",");
data.add(new Pair(columns[2], columns[3], columns[6]));
line = br.readLine();
}
}
}
// Searching for the clickthrough_url in data List
void searchInUrl(String word) {
System.out.println(" Searching word: " + word);
for (Pair line : data) {
// If word is present in the url then printing it
if (line.clickthrough_url.toLowerCase().contains(word.toLowerCase())) {
System.out.println(line.close_date + " " + line.funds_raised_percent);
}
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
TestSearch ts = new TestSearch();
ts.readData("file3.csv");
// Searching the text in the file data
ts.searchInUrl("robot");
ts.searchInUrl("fitness");
ts.searchInUrl("wearable");
}
}
File3.csv screen shot:
Microsoft Visual C# An Introduction to Object-Oriented Programming
ISBN: 978-1337102100
7th edition
Authors: Joyce Farrell