Question
Write a program to initialize the seven Wonders of the World along with their locations in two different arrays. Search for a name of the country input by the user. If found, display the name of the country along with its Wonder, otherwise display “Sorry Not Found!”
Seven wonders – CHICHEN ITZA, CHRIST THE REDEEMER, TAJMAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations – MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example – Country Name: INDIA Output: INDIA – TAJMAHAL
Country Name: USA
Output: Sorry Not Found!
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program9
{
public static void main(String[] args)
{
int locationPosition=0,i=0;
String country="";
String[] wonders = { "CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJMAHAL", "GREAT WALL OF CHINA","MACHU PICCHU", "PETRA","COLOSSEUM" };
String[] locations = { "MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN", "ITALY" };
Scanner sc = new Scanner(System.in);
System.out.print("Enter the country to be searched: ");
country = sc.next();
/* Search country in the array using linear search*/
locationPosition = -1;
for (i = 0; i < locations.length; i++)
{
if (locations[i].equals(country)==true)
{
locationPosition = i;
break;
}
}
if (locationPosition != -1)
{
System.out.println(locations[locationPosition] + " - " + wonders[locationPosition]);
}
else
{
System.out.println("Sorry location Not Found!");
}
}
}