BOARD EXAM QUESTIONS ON Arrays
BOARD EXAM QUESTIONS ON Arrays
Write a program to accept the names of 10 cities in a single dimension string array and their
STD (Subscribers Trunk Dialing) codes in another single dimension integer array. Search for
a name of a city input by the user in the list. If found, display* “Search successful” and print
the name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list”.
import java.util.*;
public class CitySearch
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String city[]=new String[10];
int std[]=new int[10];
String cityname;
int flag=0,i;
System.out.println("Enter the city names in capital and the std codes");
for(i=0;i<10;i++)
{
city[i]=sc.nextLine();
std[i]=sc.nextInt();
}
System.out.println("Enter the city to be searched");
cityname=sc.nextLine();
cityname=cityname.toUpperCase();
for(i=0;i<10;i++)
{
if(cityname.compareTo(city[i])==0)
{
System.out.println("Search Successful");
System.out.println(cityname+":"+std[i]);
flag=1;
break;
}
}
if(flag==0)
System.out.println("Search unsuccessful. No such city in the list");
}
}
Write a program to assign a full path and file name as given below. Using library functions,
extract and output the file path, file name and file extension separately as shown.
Input
C: / Users / admin / Pictures / flower.jpg
Output
path: C: / Users/admin/Pictures/
File name: flower
Extension: jpg
import java.util.*;
public class PathExtract
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int slash=s.lastIndexOf('/');
String path=s.substring(0,slash+1);
int dot=s.indexOf('.');
String filename=s.substring(slash+1,dot);
String extension=s.substring(dot+1);
System.out.println("Path - "+path);
System.out.println("File name - "+filename);
System.out.println("Extension - "+extension);
}
}
Output
Enter the string
C: / Users / admin / Pictures / flower.jpg
Path - C: / Users / admin / Pictures /
File name - flower
Extension – jpg