2013-06-02 162 views
-6

嗨,我已經完成了這個程序它工作正常,除非用戶試圖用大寫字母寫我嘗試.toUpperCase但它仍然關閉該程序,如果您嘗試使用大寫字母來搜索任何人都可以幫助請謝謝如何識別大寫字母而不只是小寫

import java.io.*; 
    import java.util.Scanner; 
    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.List; 

    public class database 
    { 
     static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     //my arrays 
     static String country [] = new String[1000]; 
     static String capital [] = new String[1000]; 
     static double population [] = new double[1000]; 

     static List<String> countriesList = Arrays.asList (country); //a new array for user to add data to 

     public static void main (String args [])throws IOException 
     { 
      // now i am adding data to the arrays 
      country[0] = "Barbados"; 
      capital[0] = "Bridgetown"; 
      population[0] = 65.3; 
      country[1] = "france"; 
      capital[1] = "paris"; 
      population[1] = 315.8; 
      country[2] = "nigeria"; 
      capital[2] = "abuja"; 
      population[2] = 170.1; 
      country[3] = "USA"; 
      capital[3] = "washington"; 
      population[3] = 2840; 
      country[4] = "japan"; 
      capital[4] = "tokoyo"; 
      population[4] = 126.7; 

      int option = 0; 

      System.out.println("WELCOME TO MY COUNTRY DATABASE\n"); 

        while(option!= 5){ //Only five options 

        options(); 
        option = Integer.parseInt(br.readLine()); 

        if(option > 5 || option < 1) 
        { 
        System.out.println("Wrong input! Try Again"); 
        System.out.println("CHOOSE FROM 1 - 5 \n "); 
       } 

        if(option == 1) { 
        addCountry(); 
        } 
        else if(option == 2){ 
        searchCountry(); //Search from an array 
        } 
        else if(option == 3){ 
        ListCountry(); 

        } 
        else if(option == 4){ 
        getFare(); //show fare to travel 
        } 
        else if(option == 5) { 
        System.out.print("\n Thank you and Goodbye "); 

       } 
      } 
      } 

     public static void options() 
     { 
      System.out.println("Main menu"); 
      System.out.println("========="); 
      System.out.println("1. Add a new country"); 
      System.out.println("2. Search for a country"); 
      System.out.println("3. Show list of countries available"); 
      System.out.println("4. Get fare from London to countries listed"); 
      System.out.println("5. Exit"); 
      System.out.print("\n Choose an option from 1 - 5: "); 
     } 

     public static void addCountry()throws IOException 
     { 
      System.out.println("\n Adding a country"); 
      System.out.println("==================="); 
      System.out.print("Enter name of country: "); 
      String countryInput = br.readLine(); 

      System.out.print("Enter Capital: "); 
      String capitalInput = br.readLine(); 

      System.out.print("Enter population: "); 
      String populationInput = br.readLine(); 

      int spareSlot = -1; 

      for (int i = 0; i < country.length; i++) // loop so data can be added to arraylist 
      { 
       if(country[i] == null) 
       { 
        spareSlot = i; 
        break; 
       } 
      } 
      country[spareSlot] = countryInput; 
      capital[spareSlot] = capitalInput; 
      population[spareSlot] = Double.parseDouble(populationInput); 

      System.out.println("\n You added the country " + countryInput + ", the capital is " + capitalInput + ", with a population of " + populationInput + "\n");  
      //System.out.println("================================================================================================================"); 

      } 

     public static void searchCountry() throws IOException 
     { 
      Scanner in = new Scanner (System.in);//Scanner to obtain input from command window 

      String output; 
      int size, i; 

      System.out.println("\n Searching countries"); 
      System.out.println("========================= \n"); 
      System.out.print("Search a Country: "); 
      output = br.readLine(); 
      boolean found = false; 
      //A loop to search from the array 
       for(i = 0; i < country.length; i++) 

       if(output.equals(country[i])) 
       { 
        found = true; 
        break; 
      } 
        if (found) 
        System.out.println(output + " is found at index " + i +"\n"); 

       else 
        System.out.println(output + ": This country is not found, choose option 1 to Add country \n"); 

        if (output == country[0]) 
       { 
         System.out.println("The capital of "+ output + "is " + capital[1] + " with a population of " + population[3]); 
       } 
        if(output == country[1]) { 
         System.out.println("The capital is" + capital[4]); 
       } 
       if(output == country[2]) { 
         System.out.println("The capital is" + capital[1]); 
       } 
        if(output == country[3]) { 
         System.out.println("The capital is " + capital[2]); 
       } 
        if(output == country[4]) { 
         System.out.println("The capital is " + capital[3]); 

       }  
     } 

     public static void ListCountry()throws IOException 
      { 
       for (String c : countriesList) 
       { 
        if(c!=null) 
         System.out.println("\n" + c +"\n"); // to list all countries so far in the array 
       } 
      } 

     public static void getFare() 
     { 
      Scanner input = new Scanner (System.in); 
      String destination; 
      int fare; 

      System.out.println("\n Get a fare:"); 
      System.out.println("============== \n"); 
      System.out.print("Select destination by entering number from 1 -5: \n"); 
      System.out.println("1 Caribbean"); 
      System.out.println("2 Europe"); 
      System.out.println("3 Africa"); 
      System.out.println("4 America"); 
      System.out.println("5 Japan"); 

      destination = input.nextLine(); //get destination from user 
      fare = Integer.parseInt(destination); 

       switch(fare) 
      { 

       case 1: System.out.println("To travel to the Carribbean the fare is from £600 \n"); 
       break; 
       case 2: System.out.println("To travel to Europe the fare is from £199 \n"); 
       break; 
       case 3: System.out.println("To travel to Africa the fareis from £500 \n"); 
       break; 
       case 4: System.out.println("To travel to America the fare is from £290 \n"); 
       break; 
       case 5: System.out.println("To travel to Japan the fare is from £550 \n"); 
       break; 
       default: System.out.println("INVALID ACTION START AGAIN \n"); 
       System.out.println("======================================"); 
      } 
     } 
     } 
+0

*如何識別大寫字母而不只是小寫* - 如果您識別小寫字母,那麼識別大寫字母有什麼問題? – Maroun

+2

請熟悉[SSCCE](http://sscce.org/)的概念 –

+0

不只是代碼解釋你在代碼中試圖做的下一個問題 – pinkpanther

回答

0

你有一個比較閱讀

if(output.equals(country[i])) 

如果你想這是不區分大小寫的,你要改變,要

if(output.equalsIgnoreCase(country[i])) 

在後面的代碼,你有類似

if (output == country[0]) 

,其比較對象的身份,而不是字符串相等線。你想改變所有這些以至少正確地比較字符串,並且可能如上所述不區分大小寫。

+0

非常感謝你 – user2433281