简体   繁体   中英

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

I bet you generated the getters and setters and the constructor for the initial set of fields which are these.

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

But after changing them to

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

you forgot to modify them accordingly. You need to change your getters and setters and constructors to something like this. The same goes with the toString() method.

public class Card {

    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[] suit, String[] name, int[] value) {
        super();
        this.suit = suit;
        this.name = name;
        this.value = value;
    }

    public String[] getSuit() {
        return suit;
    }

    public void setSuit(String[] suit) {
        this.suit = suit;
    }

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

    public void setName(String[] name) {
        this.name = name;
    }

    public int[] getValue() {
        return value;
    }

    public void setValue(int[] value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Card [suit=" + Arrays.toString(suit) + ", name="
            + Arrays.toString(name) + ", value=" + Arrays.toString(value)
            + "]";
    }
}

Always remember to generate fresh getters, setters, constructor, toString() methods, if you happen to change the fields in the class.

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

Use

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

to get string from array of string

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

in your all getter setter methods.

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.

它们是不兼容的,因为您的 get 方法是 String 和 int 类型,并且您要求这些方法返回一个数组。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM