1

When I run:

import java.util.Scanner;

public class War {

    public static void main(String[] args) {
        String[] deckcards=deck();
        String[] player1cards=player1Cards(deckcards);
    }
    public static String[] deck(){
        String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
        return Cards;
    }
    public static String [] player1Cards(String[] deckcards){
        String[] player1cards = deckcards[0];
        return player1cards;
    }

I get two errors.

One error tells me

Type mismatch cannot convert from String to String[]

The other error tells me there is a line breakpoint error on:

String[] player1cards = deckcards[0];

How can I fix this?

3 Answers 3

3

Your problem is here:

String[] player1cards = deckcards[0];

deckcards[0] is an element of an array of type String. So it is a String variable. But you are trying to assign it to String[] player1cards which is an array of strings which is not allowed in Java.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. If you have a solution to this, it would be great.
Since you are trying to assign a value of type String (deckcards[0]) to a variable, that variable has to be of type String. It will look something like this: String player1cards = deckcards[0]. You should look up simple java tutorials like this one, they explain this.
What are you trying to do with the function: public static String [] player1Cards(String[] deckcards){} ?
3

This is your error:

String[] player1cards = deckcards[0];

If you want to copy the first value of deckscards into the first value of player1cards, then one alternative is:

String[] player1cards = deckcards;
player1cards[0] = deckcards[0];

According to your code, you are saying that the whole String[] is equal to the first value of the other array. That cannot be. If you want to equal two arrays, then:

String [] automobile = {"toyota","zion"};
String[] car = {"nissan"};
automobile = car;

Hope this helps.

Comments

0

An element of a String[] is a String, not a String[], but saying that doesn't fix your problem.

Issues of design and style aside, I think your intention is:

public class War {

    public static void main(String[] args) {
        String[] deckcards=deck();
        // shuffle deck
        List<String> list = new ArrayList<>(Arrays.asList(deckcards));
        Collections.shuffle(list);
        deckcards = list.toArray(new String[]{});
        // deal from deck
        String[] player1cards=player1Cards(deckcards, 0, 5);
    }
    public static String[] deck(){
        String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
        return Cards;
    }
    public static String [] player1Cards(String[] deckcards, int from, int quantity){
        String[] player1cards = Arrays.copyOfRange(deckcards, from, from + quantity);
        return player1cards;
    }
}

Disclaimer: untested code (thumbed on iPhone) and nothing done to fix design problems, but you will have something that works.

1 Comment

Thank you. This is a huge help. +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.