I have written a block of code to assist the partial search feature when searching for regions to view house prices. However, when testing the code on the console and I search for a region that's in the CSV file being read by the code, It shows hundreds if not thousands of results. The CSV file contains records as early as 1968 till 2022, so a specific region in the CSV file is located in multiple lines as there are different records for each date. I want the code to show only the keyword results for the latest dates, and not from all the dates. Here's the code (Next message)
#Recurring region result
53 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @wanton sun! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
List<String[]> matchingRows = new ArrayList<>();
List<String[]> records = csvReader.readCSV(csvFile);
int dateIndex = -1;
int locationIndex = -1;
int valueIndex = -1;
// Find the column indices for Date, Region_Name, and Average_Price/Sales_Volume
if (!records.isEmpty()) {
String[] headerRow = records.get(0);
for (int i = 0; i < headerRow.length; i++) {
String columnName = headerRow[i];
if (columnName.equalsIgnoreCase("Date")) {
dateIndex = i;
} else if (columnName.equalsIgnoreCase("Region_Name")) {
locationIndex = i;
} else if (columnName.equalsIgnoreCase("Average_Price") || columnName.equalsIgnoreCase("Sales_Volume")) {
valueIndex = i;
}
}
}
if (dateIndex == -1 || locationIndex == -1 || valueIndex == -1) {
System.out.println("Invalid CSV format. Missing required columns.");
return matchingRows;
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date latestDate = null;
double latestValue = 0.0;
for (String[] record : records) {
if (record[locationIndex].toLowerCase().contains(location.toLowerCase())) {
try {
Date date = dateFormat.parse(record[dateIndex]);
double value = Double.parseDouble(record[valueIndex]);
if (latestDate == null || date.after(latestDate)) {
latestDate = date;
latestValue = value;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}```
matchingRows.add(new String[]{dateFormat.format(latestDate), Double.toString(latestValue)});
}
return matchingRows;
}```
List<String> matchingKeywords = new ArrayList<>();
List<String[]> records = csvReader.readCSV(csvFile);
int locationIndex = -1;
// Find the column index for Region_Name
if (!records.isEmpty()) {
String[] headerRow = records.get(0);
for (int i = 0; i < headerRow.length; i++) {
String columnName = headerRow[i];
if (columnName.equalsIgnoreCase("Region_Name")) {
locationIndex = i;
break;
}
}
}
if (locationIndex == -1) {
System.out.println("Invalid CSV format. Missing Region_Name column.");
return matchingKeywords;
}
// Search for keywords matching the partial location
for (String[] record : records) {
String location = record[locationIndex];
if (location.toLowerCase().contains(partialLocation.toLowerCase())) {
matchingKeywords.add(location);
}
}
return matchingKeywords;
}
}```
What are "latest dates"?
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
Forgot to mention, the CSV file has dates in the first column, and regions in the second column.
I made the code to show only the results of the latest dates within the CSV files.
@sick torrent able to help?
No... You've still not told about what counts as latest dates
The latest date is the last date written in the CSV file, i.e. latest records
That's the latest date, singular, yes. That much I could imagine
You're asking for the latest dates, plural
Sorry, It was late at night, I was tired.
Yes, only the latest date. That's what I need.
@sick torrent
I'm really not sure what to tell you to solve this.
I guess try explaining how something like then can be done step by step.
Then program your computer to do these steps
I just don't see where would a difficulty lie
The code that I sent was from the 'Operations' class, which conducts operations for the system. The 'Main' Class that I have is for displaying the results in the console. Here's the partial search method in the 'Main' class.
This is in a switch method because there are multiple options for the user to select, but this code is only concerned for the first option.
Explanation on how to do it step by step please. (and people prefer not to repeat what they already said)
Alright, do you want me to explain what the code does or what is present on the console?
No, what the code should do to achieve that task.
Alright. The code uses the 'Reader' class to read the CSV files. In the 'Operations' class, the 'search' method conducts a searching functionality on the loaded data from the CSV file (the CSV file is loaded in the 'Main' class, where the code would then use the 'Operations' class to conduct the search). In the 'Main' class, a partial search functionality is added, where the console would show multiple results if the user were to enter a missing or incomplete keyword. This is shown in the last screenshot I sent, at the top of it. Now, what I expect the code to do is to show the matching regions from the last dates. However, the code would display the regions taken from all the dates, and when the user enters one of the regions, it would show the latest records on date for the selected region. Can I show you a test run?
What I think should happen is that there should be a limit on the recurring results, i.e. the console should show the name of the region once only.
I don't need examples, I need to understand what is your plan to do that search task
You're supposed to know what you want your computer to do, and then transmit these orders in the form of programming code
So, what is it that you intend to make your computer do?
I want it to show the names of the regions as a list in case the user didn't put in the full name of the region they want.
The final result would be that it shows the latest record of average pricing and sales volume of houses in that region
And what are the detailed steps you intend to command it so that the end result is that it does that?
(I shouldn't have to ask that question that much, you're supposed to understand the first time I ask for detailed steps)
I want the code to stop recurring the names of the regions in the results by setting a limit of one name instead of many. I don't know how to explain it further. What I'm asking is for the implementation, of how I should make that possible.
One second.
@sick torrent Here's a different intention, I want the code to keep track of the regions that have already been displayed, like saving it in a list. Before displaying the results on the console, the code would check if the region is recurring or already displayed in the list. If it is, then the code would skip displaying the recurring region and shows only one result per region in the partial search. Is that good enough?
Would that be implemented in the 'Main' class or the 'Operations' class?
First I don't understand this recurring thing, second that just have nothing to do with your opening question
I'll show you an example then.
What I think is happening here is that it took all the names from the CSV file and shown it in the console. The CSV file contains records of house pricing from 1968 till 2022, and the regions are shown multiple times in the CSV file because the records of house pricing change over time.
So instead of what's happening in the screenshot, I want it to show:
- England
- East of England
- England and Wales
@sick torrent Still there?
@wanton sun 2 questions:
In the partialSearch function, I could not see any date-wise filtering. So how do you expect to see only the latest records when records List contains all the records?
If you dont want to see recurring results, why don't you use a HashSet instead of ArrayList for matchingKeywords?
I do not know about HashSet, and I don't know how to implement a date-wise filtering. If you have a reference or a tutorial that you can send, I'd appreciate it.
Ok, lets put it this way,
If you just want to remove the recurring results, forget about HashSet, remove the duplicates from the matchingKeywords List before returning it from the partialSearch function.
// return matchingKeywords;
return matchingKeywords.stream().distinct().collect(Collectors.toList());
If you want to return only the results from the latest dates then you will have to do something similar to your search function. Find the region_name and date index in the records and then do
if date.after(latestDate) {
matchingKeywords.add(location);
}
I'll try implementing this, I'll come back later on with feedback, thanks for the quick response.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
I used the first code snippet because whenever you select any of the choices before the fix, it will only return the latest records, no matter what selection you make. The recurring names was the only problem. Thanks for your help.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.