JAVA的帮助,我似乎无法找到错误

时间:2015-05-22 19:34:49

标签: java arrays

我是java的初学者,我一直在玩数组,但我无法理解什么是错误的。

这是班级:

public class StringArrayUtil {

        public static void print(String [] sArray){
            for(int i = 0; i<sArray.length; i++){
                if(sArray[i] !=null){
                    System.out.println("Indice: "+i+ " String: "+sArray[i] + "\t");
                }
            }
        }

        public static int indexOf(String [] sArray, String sSearch, int s){
            for(int i = s; i < sArray.length; i++){
                if(sArray[i] != null && sSearch != null && sSearch.equals(sArray[i])){
                    return i;
                }
            }   
            return -1;
        }

        public static int indexOf(String [] sArray, String sSearch){
            return indexOf(sArray, sSearch, 0);

        }

        public static int indexOfEmpty(String[] sArray){
            return indexOf (sArray,null,0);
        }

        public static int put(String[] sArray, String newS){
            for(int i = 0; i<sArray.length; i++){
                if(sArray[i] == null){
                    sArray[i]=newS;
                    return i;
                }
            }
            return -1;
        }

        public static int remove(String sArray, String removeS){
            int times = 0;
            for(int i = 0; i < sArray.length; i++){
                if(removeS.equals(sArray[i]) ){
                    sArray[i] = null;
                    times ++;
                }
                return times;

            }
         }

        public static String fillArray (String messege, int n){
            String[] result = new String[n];
            for(int i = 0; i<result.length; i++){
                result[i] = Scanner.getString (messege);
            }
            return result;
        }
}

这是我的主要内容:

class StringArrayUtilTester {
    public static void main(String [] args){
        System.out.println("----------Welcome to StringMaster 2.0----------");
        int z=5;
        String a= "-Create a String: ";
        String [] array = StringArrayUtil.fillArray(a,z);
        String search = Scanner.getString("-String SearchBox! find and delete String: ");
        int i = StringArrayUtil.indexOf(array, search);
            if (i != -1){
                System.out.println("-The String belongs to the Array!");
                array[i] = null;
                System.out.println("...loading....");
                System.out.println("-The string has been deleted.");

            }
            else{
                System.out.println("-String not found, try another one!");
            }

        String replace = Scanner.getString("-Replace with new string: ");
        int e = StringArrayUtil.indexOfEmpty(array);
            if(e != -1){
                array[e] = replace;
            }

        System.out.println("-Final Array of Strings: ");
        StringArrayUtil.print(array);

    }
}

我尝试javac时无法理解的错误。我没有正确填写fillArray吗? :

StringArrayUtilTester.java:7: error: incompatible types: String cannot be converted to String[]
    array =  StringArrayUtil.fillArray(a,z);

4 个答案:

答案 0 :(得分:6)

numbers = [] with open("1.txt", 'r') as f: for line in f: nums = line.split() for n in nums: numbers.append(str(int(n)**2)) 方法的返回类型为fillArray,不是String

您需要通过更改返回类型使方法返回String[]String[]

答案 1 :(得分:3)

fillArray返回String。 您应该将原型更改为public static String[] fillArray (String messege, int n){

答案 2 :(得分:2)

fillArray的返回类型应为String []

     public static String[] fillArray (String messege, int n){
        String[] result = new String[n];
        for(int i = 0; i<result.length; i++){
            result[i] = Scanner.getString (messege);
        }
        return result;
    }

答案 3 :(得分:2)

方法fillArray

中的两个问题
  1. 该方法需要String,但您返回String[]。方法签名可能应为String[]

  2. Scanner没有静态方法getString

  3. 您需要在使用之前创建Scanner的实例:

    Scanner scan = new Scanner(System.in);
    

    然后你可以做

    result[i] = scan.next();