将字符串添加到集合并显示它

时间:2013-05-29 16:14:10

标签: java

我正在为用户编写一个程序,将一个字符串添加到ArrayList然后显示它。 它不起作用,似乎compareTo()存在问题。

这是我的代码:

public class database {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String country[] = new String[100];
    static String capital[] = new String[100];
    static double population[] = new double[100];
    static List<String> countriesList = Arrays.asList(country);

    public static void main(String args[]) throws IOException {
        country[0] = "Barbados";
        country[1] = "France";
        country[2] = "Nigeria";
        country[3] = "USA";
        country[4] = "Japan";
        capital[0] = "Bridgetown";
        capital[1] = "Paris";
        capital[2] = "Abuja";
        capital[3] = "Washington";
        capital[4] = "Tokyo";
        population[0] = 65.3;
        population[1] = 315.8;
        population[2] = 170.1;
        population[3] = 2840;
        population[4] = 126.7;

    public static void searchCountry() throws IOException {
        Scanner in = new Scanner(System.in);
        String output;
        int size, i;
        System.out.println("Search Country:");
        output = br.readLine();
        boolean found = false;
        for (i = 0; i < country.length; i++)
            if (output.compareTo(country[i]) == 0) {
                found = true;
                break;
            }
        if (found)
            System.out.println(output + " is found at index " + i);
        else
            System.out.println(output + "Country not found, choose Add country to add it");

    public static void listCountry() throws IOException {
        for (String c : countriesList) {
            if (!=null)
            System.out.println(c);

        }
    }
}

我的代码末尾的null也存在问题。

3 个答案:

答案 0 :(得分:1)

编写代码时,最好从头开始。即。

首先,编写类名并确保括号

没有问题
public class MyClass{
}

然后,在其中编写main方法。

public class MyClass{
    public static void main(String[] args){
    }
}

然后,编写您的方法,并在main方法中进行测试。

public class MyClass{
    public void mymethod(){
        //do something
        System.out.println("say something");
    }
    public static void main(String[] args){
         MyClass mc = new MyClass();
         mc.mymethod();
    }
}

如果你试图在一个镜头中写下所有东西它就不会起作用,如果你不是专家那么你就很难解决问题。

答案 1 :(得分:0)

  1. 您无法在方法中编写方法。在打开main()
  2. 的括号之前,请关闭searchCountry()的括号
  3. 您不会检查任何内容为null。也许你的意思是if(c != null)
  4. 只要这样写,你应该没问题:

    public class database {
    
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        static String country[] = new String[100];
        static String capital[] = new String[100];
        static double population[] = new double[100];
        static List<String> countriesList = Arrays.asList(country);
    
        public static void main(String args[]) {
            country[0] = "Barbados";
            country[1] = "France";
            country[2] = "Nigeria";
            country[3] = "USA";
            country[4] = "Japan";
            capital[0] = "Bridgetown";
            capital[1] = "Paris";
            capital[2] = "Abuja";
            capital[3] = "Washington";
            capital[4] = "Tokyo";
            population[0] = 65.3;
            population[1] = 315.8;
            population[2] = 170.1;
            population[3] = 2840;
            population[4] = 126.7;
            searchCountry();
            listCountry();
        }
    
        public void searchCountry() throws IOException {
            Scanner in = new Scanner(System.in);
            String output;
            int size, i;
            System.out.println("Search Country:");
            output = br.readLine();
            boolean found = false;
            for (i = 0; i < country.length; i++)
                if (output.compareTo(country[i]) == 0) {
                    found = true;
                    break;
                }
            if (found)
                System.out.println(output + " is found at index " + i);
            else
                System.out.println(output + "Country not found, choose Add country to add it");
        }
    
        public void listCountry() {
            for (String c : countriesList) {
                if (c!=null)
                    System.out.println(c);
            }
        }
    }
    

答案 2 :(得分:0)

您忘记了c中的c != null
您无法在java中定义方法内部的方法。
另外,在测试equals相等时使用String方法,即if (output.equals(country [i]))