-9

I'm new to java programming language, could somebody please tell me the reason for following error.

error:

incompatible types: java.lang.String[] cannot be converted to java.lang.String

public class StringArray {

        String[] name = {"abc", "def", "ghi"};

        public void setStringArray() {
            for (int i = 0; i < name.length; i++) {
                if (name.equals(i)) {
                    System.out.println("print");
                } else {
                    System.out.println("wrong");
                }
            }

        }

        public String getStringArray() {

            return name;
        }



        public static void main(String[] args) {
            StringArray sa = new StringArray();
            sa.setStringArray();
            System.out.println(sa.getStringArray());

        }
    }
user306128
  • 101
  • 4
  • 4
    You also don't need to mention that you're new to Java. It doesn't affect the way your question is treated, and it's obvious from the question. – Kayaman Jun 10 '16 at 05:39
  • If you can read English,then the error message it self 'saying' what is wrong. – Mehraj Malik Jun 10 '16 at 06:03

2 Answers2

1

You need to change two things. First, the method

public String getStringArray() {
    return name;
}

Is invalid because name is of type String[], not String. This is correct:

public String[] getStringArray() {
    return name;
}

Second of all, your for loop logic is wrong.

        for (int i = 0; i < name.length; i++) {
            if (name.equals(i)) {
                System.out.println("print");
            } else {
                System.out.println("wrong");
            }
        }

This checks if a String[] is equal to an integer. That's not correct logic. You need to get the index by doing name[i] then do something like a try catch statement to parse the integer from the string.

        for (int i = 0; i < name.length; i++) {
            try {
                var nameInt = Integer.parseInt(name[i]);
                if (nameInt.equals(i)) {
                    System.out.println("print");
                } else {
                    System.out.println("wrong");
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

You can refer to a question like this for a more thorough look into parsing integers from strings.

Just a note: when you call sa.setStringArray(); it does nothing because the string array is hardcoded.

Community
  • 1
  • 1
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Under what circumstances do you think a `String` will be equal to an `Integer`? Why do you think this is less wrong than the original? – Andy Turner Jun 10 '16 at 06:29
-1

In below method you are trying to return an array of string but the return type of your method is "String" and it is incompatible because you can't typecast an array of String to String.

public String getStringArray() {
   return name;
    }

instead use this

public String getStringArray(){ 
    return name.toString();
}
  • 2
    Calling `toString()` on an array is basically useless, especially when the method name implies that it returns an array. – Andy Turner Jun 10 '16 at 06:37