Pafmac
Pafmac

Reputation: 3

JAVA help, I cant seem to find the error

I'm a beginner at java, i have been playing around with arrays, but I can't understand whats going wrong.

This is the class:

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;
        }
}

And this is my main:

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);

    }
}

The errors I get when I try to javac i cant understand. Am I not doing my fillArray correctly? :

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

Upvotes: 0

Views: 118

Answers (4)

gtgaxiola
gtgaxiola

Reputation: 9331

Two problems in the method fillArray

  1. The method is expecting a String but you return a String[]. Probably the method signature should be String[]

  2. Scanner doesn't have a static method getString

You need to create an instance of Scanner prior to using it:

Scanner scan = new Scanner(System.in);

Then you can do

result[i] = scan.next();

Upvotes: 2

Shantanu Deshpande
Shantanu Deshpande

Reputation: 224

The return type of fillArray should be 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;
    }

Upvotes: 2

D. Ben Knoble
D. Ben Knoble

Reputation: 4673

The return type on your fillArray method is String which isn't String[]

You need to make the method return a String[] by changing the return type: public String[] fillArray(...)

Upvotes: 7

Anindya Dutta
Anindya Dutta

Reputation: 1982

fillArray returns a String. You should change the prototype to public static String[] fillArray (String messege, int n){

Upvotes: 3

Related Questions