Hi struggling with java - we have a class Structures and Algorithims
in which we are supposed to pick up java in 5 weeks
Have been pouring over material and searching sites but trouble is when I start to write ends up nothing seems to work unless we copy and paste from the text - very frustrating noone to ask what happens if i do it this way or why does etc etc not work

For example we had to do the card hand thing for assessment
which is now done and dusted
I tried to do a string array and found the multidimensional string
now assessment is over still want to know how to do it and cannot understand why the below does not work / compile

import java.util.*;
public class Card
{
    private String[][] card={{"Hearts","Clubs","Diamonds","Spades"},
                    {"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}};
    final int SUITE_NUM = 4;
    final   int CARD_VAL_NUM = 13 ;


    public Card()
    {
     Card card=new Card();
    int x = (int)(Math.random()*(SUITE_NUM))+1;
    int y =(int)(Math.random()*(CARD_VAL_NUM))+1;
     this.card = card[0][x]+ card[1][y] ;

    }
    public String toString(){ return this.card +"," ;   }


}

o c++ is soo much easier to understand .. sigh

any input most sincerely welcome and hope you do not mind if i come back with more since i do not seem to be getting any answers in class - thanks in advance

sorry error message is

Card.java:15: array required, but Card found
this.card = card[0][x]+ card[1][y] ;
^
Card.java:15: array required, but Card found
this.card = card[0][x]+ card[1][y] ;
^
Card.java:15: incompatible types
found : java.lang.String
required: java.lang.String[][]
this.card = card[0][x]+ card[1][y] ;
^
3 errors

First of all don't do this:

public Card() {
  Card card = new Card();
}

When you try to create an object you will call the constructor. Inside it you call this: Card card = new Card(); , which will call the constructor, but inside it you call this: Card card = new Card(); , which will call the constructor, but inside it you call this: Card card = new Card(); , which will call .......

Do you see where I am getting to?

Also about your error. The this.card is an array, but these:
card[0][x], card[1][y] are Strings, so you can't assign a String to an array:

String [][] card;
card = "Clubs"+"Six";

Thanks for quick response

OK so now I have

    public Card()
    {
    final int SUITE_NUM = 4;
    final   int CARD_VAL_NUM = 13 ;
    String[][] pack={{"Hearts","Clubs","Diamonds","Spades"},
        {"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}};
        int x =(int)(Math.random()*(SUITE_NUM))+1 ;
        int y =(int)(Math.random()*(CARD_VAL_NUM))+1 ;
        this.card = pack[0][x] + pack[1][y] ;
    }


}

which gives error

Card.java:13: incompatible types
found   : java.lang.String
required: java.lang.String[][]
        this.card = pack[0][x] + pack[1][y] ;
                               ^
1 error

but this works fine what is difference please?

String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                        {"Smith", "Jones"}};
            int x = (int)(Math.random()*(2))+1;
            int y =(int)(Math.random()*(1))+1;
    System.out.println(names[0][x] + names[1][y]); //Mr. Smith
    System.out.println(names[0][x] + names[1][y]); //Ms. Jones

Thanks for quick response

OK so now I have

    public Card()
    {
    final int SUITE_NUM = 4;
    final   int CARD_VAL_NUM = 13 ;
    String[][] pack={{"Hearts","Clubs","Diamonds","Spades"},
        {"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}};
        int x =(int)(Math.random()*(SUITE_NUM))+1 ;
        int y =(int)(Math.random()*(CARD_VAL_NUM))+1 ;
        this.card = pack[0][x] + pack[1][y] ;
    }


}

which gives error

Card.java:13: incompatible types
found   : java.lang.String
required: java.lang.String[][]
        this.card = pack[0][x] + pack[1][y] ;
                               ^
1 error

but this works fine what is difference please?

String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                        {"Smith", "Jones"}};
            int x = (int)(Math.random()*(2))+1;
            int y =(int)(Math.random()*(1))+1;
    System.out.println(names[0][x] + names[1][y]); //Mr. Smith
    System.out.println(names[0][x] + names[1][y]); //Ms. Jones

end quote.

I have already explained why, and the error tells you exactly that:
this.card is an array and these pack[0][x], pack[1][y] are Strings. You cannot assign a String to an array.

i give up - you can't assign string values to an array?

you can't make new array and assign a few of those to another array

isn't that what this has all been about?

if i can assisgn methods to array surely i can assign string values

is it a library, is it a function.....no it's java

i give up - you can't assign string values to an array?

An array is an array and a String is a String. How can you expect to do this:
array = String this.card = pack[0][x]; card is an array. pack is an array.
card[x][y] is a String. pack[x][y] is a String.


you can't make new array and assign a few of those to another array

That doesn't make any sense.


if i can assisgn methods to array surely i can assign string values

You cannot assign a method to an array. A method returns an array.

public String [] methodArray() {
  String [] arr = {"a", "b"};
  return arr;
}

String [] aNewArray = methodArray();

The methodArray returns an array and you put the result that it returns to an array.
In the same way a method returns an int:

int methodInt() {
   return 5;
}

int i = methodInt();

If you want to put the values of one array into an other then
Create a new array that has the same length as the original.
Then write a for loop. Loop the array and put each of the elements of one array into the other.

It would be helpful if ask specific questions on what you are trying to achieve

Thanks for quick response

OK so now I have

    public Card()
    {
    final int SUITE_NUM = 4;
    final   int CARD_VAL_NUM = 13 ;
    String[][] pack={{"Hearts","Clubs","Diamonds","Spades"},
        {"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}};
        int x =(int)(Math.random()*(SUITE_NUM))+1 ;
        int y =(int)(Math.random()*(CARD_VAL_NUM))+1 ;
        this.card = pack[0][x] + pack[1][y] ;
    }


}

which gives error

Card.java:13: incompatible types
found   : java.lang.String
required: java.lang.String[][]
        this.card = pack[0][x] + pack[1][y] ;
                               ^
1 error

but this works fine what is difference please?

String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                        {"Smith", "Jones"}};
            int x = (int)(Math.random()*(2))+1;
            int y =(int)(Math.random()*(1))+1;
    System.out.println(names[0][x] + names[1][y]); //Mr. Smith
    System.out.println(names[0][x] + names[1][y]); //Ms. Jones

end quote.

I would also like to add something that I didn't mention and might give you an extra help:

This:

this.card = pack[0][x] + pack[1][y];

Is putting the String: pack[0][x] + pack[1][y] into an array.

This:

System.out.println(names[0][x] + names[1][y]);

Is printing the String: names[0][x] + names[1][y]

Hey thanks I think

What I was trying to do was set up classes for hand of cards which had to be dealt and then sorted

Guess I could create the whole deck into one array and then deal randomly using the index for random also check index not repeated

Having major blank screen probs at moment and have had to give up class but hoping to teach myself some java from the start and then give it another go next year

Hope you guys don't mind my coming back for tips and advice when finally resolve this prob trouble is that the java used in class just doesn't seem to quite match the stuff in tutorials online and in the end the only solution is to copy and paste tutors solution which don't really make me feel I have learned anything - was all about structure and algorithim so not sure why is not being taught in seudo code but aparently i am alone in this thinking

anyway thanks and will give it a try
i will be back :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.