October 22, 2009 at 11:40 AM
Hi Friend,
Try the following codes:
1)
import java.util.*;
public class FindCapital {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] states = {"Andhra Pradesh", "Arunachal Pradesh", "Assam","Bihar", "Chhattisgarh", "Goa", "Gujarat", "Haryana", "Himachal Pradesh","Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra","Manipur", "Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab", "Rajasthan", "Sikkim","Tamil Nadu", "Tripura", "Uttar Pradesh","Uttarakhand", "West Bengal"};
String[] capitals = {"Hydrabad", "Itanagar", "Dispur","Patna", "Raipur", "Panaji", "Gandhinagar","Chandigarh", "Shimla", "Srinagar", "Ranchi","Bengaluru", "Thiruvananthapuram", "Bhopal","Mumbai","Imphal", "Shillong","Aizawl", "Kohima", "Bhubaneswar", "Chandigarh","Jaipur", "Gangtok", "Chennai", "Agartala","Lucknow", "Dehradun", "Kolkata"};
for(int i=0;i<states.length;i++){
int j=i+1;
System.out.println(j+". "+states[i]);
}
System.out.print("Enter your choice: ");
int value = scanner.nextInt();
String capital = "";
switch(value){
case 1:
capital = capitals[0];
break;
case 2:
capital = capitals[1];
break;
case 3:
capital = capitals[2];
break;
case 4:
capital = capitals[3];
break;
case 5:
capital = capitals[4];
break;
case 6:
capital = capitals[5];
break;
case 7:
capital = capitals[6];
break;
case 8:
capital = capitals[7];
break;
case 9:
capital = capitals[8];
break;
case 10:
capital = capitals[9];
break;
case 11:
capital = capitals[10];
break;
case 12:
capital = capitals[11];
break;
case 13:
capital = capitals[12];
break;
case 14:
capital = capitals[13];
break;
case 15:
capital = capitals[14];
break;
case 16:
capital = capitals[15];
break;
case 17:
capital = capitals[16];
break;
case 18:
capital = capitals[17];
break;
case 19:
capital = capitals[18];
break;
case 20:
capital = capitals[19];
break;
case 21:
capital = capitals[20];
break;
case 22:
capital = capitals[21];
break;
case 23:
capital = capitals[22];
break;
case 24:
capital = capitals[23];
break;
case 25:
capital = capitals[24];
break;
case 26:
capital = capitals[25];
break;
case 27:
capital = capitals[26];
break;
case 28:
capital = capitals[27];
break;
default:
capital = "Invalid choice.";
}
System.out.println("Its capital is: " + capital);
}
}