need in Java, either put psedocode with execution CHAPTER 10 Buil d an array cal
ID: 3714591 • Letter: N
Question
need in Java, either put psedocode with execution
CHAPTER 10 Buil d an array called cities with 5 elements. To populate the array prompt the user to enter a city. After entering the five cities, display the contents of the array Also after prompting the user to enter a city, build a sequential search for the city requested. If the city is located, display it, but if the city is not located, display an error message. Be sure to test the program with both possibilities. This assienment will be due Monday, May 7h Saurc coExplanation / Answer
Code
import java.io.*;
import java.util.Scanner;
class test {
public static void main (String[] args) {
String[] cities = new String[5];
Scanner sc=new Scanner(System.in);
for(int i = 0; i<5 ;i++){
System.out.println("Enter the city "+ (i+1) + ":");
cities[i] = sc.nextLine();
}
System.out.println("Cities entered are: ");
for(String city : cities)
System.out.println(city);
System.out.println("Enter the city name to be searched :");
String cityToBeSearched = sc.nextLine();
boolean flag = false;
for(String city: cities){
if(city.equals(cityToBeSearched)){
System.out.println(city);
flag = true;
break;
}
if(flag) break;
}
if(!flag)
System.out.println("City not found");
}
}
Output
possibility 1 when city is present
Enter the city 1: New Delhi
Enter the city 2: Mumbai
Enter the city 3: Bangalore
Enter the city 4: Pune
Enter the city 5: Hyderabad
Cities entered are:
New Delhi
Mumbai
Bangalore
Pune
Hyderabad
Enter the city name to be searched : Pune
Pune
possibility 2 when city not found
Enter the city 1: New Delhi
Enter the city 2: Mumbai
Enter the city 3: Bangalore
Enter the city 4: Pune
Enter the city 5: Hyderabad
Cities entered are:
New Delhi
Mumbai
Bangalore
Pune
Hyderabad
Enter the city name to be searched : Pune
City not found
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.