'JAVA - Error incompatible types: String cannot be converted to String[ ]

I'm working on Java project - Playing Cards.

And, in this Card.java class, I've declared and initialized variables and array list. I've come across this error. These errors are under each getter and setter methods.

error: incompatible types: String cannot be converted to String[]
error: incompatible types: int[] cannot be converted to int

public class Card {

   // private String suit;
    //private String name;
    //private int value;

private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Jack","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};

public Card(String s, String n, int v)
{
    suit = s;
    name = n;
    value = v;
}

public String getSuit()
{
    return suit;
}

public String getName()
{
    return name;
}

public int getValue()
{
    return value;
}

public String toString()
{
    return "<"+suit+" "+name+">";
}

This is the whole class.

Hope anyone knows and can help me out, thanks! (:

If you don't understand what i'm trying to get at, let me know, try my best to explain



Solution 1:[1]

Your variables suit, name and value are arrays:

private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Jack","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};

In the Constructor you are passing String s , String n, int v as a single data type and assigning it to an array suit, name and value.

public Card(String s, String n, int v)
{
    suit = s;
    name = n;
    value = v;
}

Make them String[] s, String[] n , int[] v your error will be gone:

public Card(String[] s, String[] n, int[] v)
{
    suit = s;
    name = n;
    value = v;
}

In the below code you are getting an array not a single string. Make their return type String[]

public String getSuit()
{
    return suit;
}

public String getName()
{
    return name;
}

Solution 2:[2]

Use

for(String s:suit){
System.out.println(s);
}

to get string from array of string

Solution 3:[3]

Use String[] instead of String and Int[] instead of int

in your all getter setter methods.

Solution 4:[4]

You are returning the array in get methods and you have declared its return type String, it should be array of Strings.

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

You can do it for others similarly.

Solution 5:[5]

They are incompatible because your get methods are of type String and int, and you are asking these methods to return an array.

Solution 6:[6]

The assignment of variables suit, name and value in the Card constructor is wrong. The variables suit , name and value are of array type and you are assigning string and int type to it., which is wrong. In Java, if the size of array is fixed, it cannot be modified. You can use array list instead as,

import java.util.ArrayList;
import java.util.Arrays;

public class Card {

    private static ArrayList<String> suitArrayList = new ArrayList<String>();
    private static ArrayList<String> nameArrayList = new ArrayList<String>();
    private static ArrayList<Integer> valueArrayList = new ArrayList<Integer>();

    private static String[] suit = {"spades","hearts","clubs","diamonds"};
    private static String[] name = {"Ace","Jack","Queen","King"};
    private static Integer[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};

    public Card(String s, String n, int v)
    {
        suitArrayList.add(s);
        nameArrayList.add(n);
        valueArrayList.add(v);
    }

    public static void main(String[] args) {
        suitArrayList.addAll(Arrays.asList(suit));
        nameArrayList.addAll(Arrays.asList(name));
        valueArrayList.addAll(Arrays.asList(value));
        Card card = new Card("ADDED SUITE", "ADDED NAME", 222222);
        for (String s : suitArrayList) {
            System.out.println("suit element :: " + s);
        }
        for (String s : nameArrayList) {
            System.out.println("name element :: " + s);
        }

        for (Integer i : valueArrayList) {
            System.out.println("value element :: " + i);
        }
    }
}

Solution 7:[7]

This Error incompatible types: String cannot be converted to String[ ]

Answer: The above ERROR came when the JAVA Env had some interruption while accessing the java default library. There are 2 ways to figure out this issue: 1] Reinstall the JDK and reset the JAVA Env path. Still, if you having issues or ERROR then go with the given approach.

2] Declare the String class with its package name: Ex: java.lang.String[] str = “SarfarazShaikh”; So, This will work as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oHo
Solution 2 raj
Solution 3 Tanu Garg
Solution 4 Devavrata
Solution 5 Henry
Solution 6 AJJ
Solution 7 Sarfaraz Shaikh