Question
Could someone please fix my code? I'm supposed to use binary search to search for the country and its information by name, and linear search
Could someone please fix my code? I'm supposed to use binary search to search for the country and its information by name, and linear search to search for the capital and its related information. But my code won't work. I think it's mainly because I can't get my binary search to work properly.
Here in my completed country.java:
public class Country { private String name, capital; private int population;
public Country(String n, String c, int p) {
name = n; capital = c; population = p; } public void setName(String n) { name = n; } public void setCapital(String c) { capital = c; } public void setPopulation(int p) { population = p; } public String getName() { return name; }
public String getCapital() { return capital; }
public int getPopulation() { return population; } public String toString() { return "Country:" + name + "Capital:" + capital + "Population:" + population; } }
Here is my CountryFinder.java that I need help with:
import java.io.*;
import java.util.*;
public class CountryFinder {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(new FileReader("countries.txt"));
Country[] arr = new Country[196];
int i = 0;
while (input.hasNextLine()) {
String n = input.nextLine();
String c = input.nextLine();
int p = Integer.parseInt(input.nextLine());
Country next = new Country(n, c, p);
arr[i] = next;
i++;
}
input.close();
search(arr);
}
public static int linSearch(Country[] array, String key) {
for (int i = 0; i < array.length; i++) {
Country country = array[i];
if (country.getName().equalsIgnoreCase(key) || country.getCapital().equalsIgnoreCase(key)) {
return i;
}
}
return -1;
}
public static int binSearch(Country[]array, String key)
{
for (int i = 0; i< array.length; i++)
{
int left = 0; //left index of subarray to search in
int right = array.length-1; //right index of subarray to search in
while(left <= right)
{
int mid = (left+right)/2; //find index in the middle of subarray
if(array[i] == key)
return i; //we found it return its index
if(key < array[mid])
right = i - 1; //search in left side
else
left = i + 1; //seach in right side
Country country = array[i];
if (country.getName().equalsIgnoreCase(key) || country.getCapital().equalsIgnoreCase(key)) {
return i;
}
return -1; //key not found in array
}
public static void search(Country[] array) {
Scanner keyboard = new Scanner(System.in);
int choice = 1;
while (true) {
displayMenu();
choice = keyboard.nextInt();
keyboard.nextLine();
if (choice == 0) {
break;
} else if (choice == 1) {
System.out.print("Enter country name: ");
String countryName = keyboard.nextLine().trim();
int getIndex = binSearch(array, countryName);
if (getIndex > 0) {
Country country = array[getIndex];
System.out.println(country.getName() + " Capital: " + country.getCapital() + " Pop: " + country.getPopulation());
} else {
System.out.println("No country found for such a name");
}
} else if (choice == 2) {
System.out.print("Enter capital name: ");
String countryName = keyboard.nextLine().trim();
int getIndex = linSearch(array, countryName);
if (getIndex > 0) {
Country country = array[getIndex];
System.out.println(country.getName() + " Capital: " + country.getCapital() + " Pop: " + country.getPopulation());
} else {
System.out.println("No capital found for such a name");
}
} else if (choice == 3) {
int largestPopulation = 0;
Country country = array[0];
for (Country c : array) {
if (c.getPopulation() > largestPopulation) {
country = c;
largestPopulation = c.getPopulation();
}
}
System.out.println(country.getName() + " Capital: " + country.getCapital() + " Pop: " + country.getPopulation());
} else if (choice == 4) {
int smallestPopulation = Integer.MAX_VALUE;
Country country = array[0];
for (Country c : array) {
if (c.getPopulation() < smallestPopulation) {
country = c;
smallestPopulation = c.getPopulation();
}
}
System.out.println(country.getName() + " Capital: " + country.getCapital() + " Pop: " + country.getPopulation());
} else if (choice == 5) {
System.out.print("Enter minimum population: ");
int min = keyboard.nextInt();
for (Country c : array) {
if (c.getPopulation() >= min) {
System.out.println(c.getName() + " Capital: " + c.getCapital() + " Pop: " + c.getPopulation());
}
}
} else if (choice == 6) {
System.out.print("Enter maximum population: ");
int max = keyboard.nextInt();
for (Country c : array) {
if (c.getPopulation() <= max) {
System.out.println(c.getName() + " Capital: " + c.getCapital() + " Pop: " + c.getPopulation());
}
}
}
}
System.out.println("Goodbye!");
}
public static void displayMenu() {
System.out.println
("Choose an option: " +
"1. Search country by name " +
"2. Search country by capital " +
"3. Find country with the largest population " +
"4. Find country with the smallest population " +
"5. List countries with over _____ people " +
"6. List countries with under _____ people " +
"Type 0 to quit");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started