Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello! Once again i turn to these forums for help...... ive just started out learning java and im trying to develop a blackJack application but am having difficulties. The difficulties ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    59

    Question Java question...... problems with my code...


    Hello! Once again i turn to these forums for help...... ive just started out learning java and im trying to develop a blackJack application but am having difficulties. The difficulties lie in my ability to develop a random card method.... i know this is a stupid question but why does this code return "Syntax error, insert "AssignmentOperator ArrayInitializer""?
    Code:
    	//random 
    	int random = (int)Math.floor(Math.random() * 2);
    	
    
    	
    	//Cards
    	Vector dealtCards = new Vector();
    	String[] cards = {"Ace", "2"};
    	
    	public String randomCard() {
    		cards[random];
    	}
    I know this is a stupid question but any help would be very greatful!!
    thanks in advance,
    tom

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Is that your entire set of code? Pieces seem to be missing, such as a main() method. Just from looking at it here's some little things I noticed. First of all your randomCard() method has a return type of String but it's not actually returning anything. This code doesn't do anything:
    Code:
    cards[random];
    It seems like what you're trying to do is get a String value from your cards array, but you're not assigning the returned value you get to anything. What you would want is something like this:
    Code:
    String anyCard = cards[random];
    return anyCard;
    That will bring back something and assign it to anyCard, and your randomCard() method will now return what it's supposed to return.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    mzq
    mzq is offline
    Just Joined!
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    26
    You do not need anyCard.
    Just "return cards[random];" should be enough and is also cleaner.

  4. #4
    mzq
    mzq is offline
    Just Joined!
    Join Date
    Aug 2006
    Location
    Germany
    Posts
    26
    Your "random function" is also very unclean. Please have a look at java.util.Random for such a job.

    Regards.

  5. #5
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Also, I recommend you use List instead of Vector:
    Code:
    List<Card> dealt = new ArrayList<Card>(); // Java 1.5
    List dealt = new ArrayList(); // Java < 1.5
    There's not much of a difference, but List is part of the Collections framework and Vector is also designed with sychronization in mind.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  6. #6
    Just Joined!
    Join Date
    Aug 2006
    Posts
    59
    alright everyone, i cannot thankyou all enough for the help........ but im having trouble with my card class that im making to store the value, suite, and number. here is the code
    Code:
    	public class card {
    		int numberCard;
    		int value;
    		String suite;
    		
    		
    		String displaySuite() {
    			return suite;
    		}
    		
    		int cardNumber() {
    			return numberCard;
    		}
    		
    		int value() {
    			return value;
    		}
    	}
    	
    	//Cards
    	
    	card card1 = new card();
    	card1.numberCard = 1;
    	card1.value = 11;
    	card1.suite = "hearts";
    It returns syntax errors on:

    card1.numberCard = 1;
    card1.value = 11;
    card1.suite = "hearts";

    tom

  7. #7
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Ok, this is mostly a matter of style, but here goes. The name of a class should be capitalized, so in your case the declaration should be:

    Code:
    public class Card {
    When declaring methods, those methods should have qualifiers such as public or private, depending on whether you want anyone outside of the class to be able to access them. Also, the names for getter methods and setter methods are generally just that:

    Code:
    public String getSuite() { return suite; }
    public int getNumberCard() { return numberCard; }
    public int getValue() { return value; }
    Finally, it's not good practice to directly manipulate values inside of a class. By default, Java makes all internal attributes in a class private to prevent this. Instead, make a setter method that will do that for you:

    Code:
    public void setSuite(String newSuite) { this.suite = newSuite; }
    public void setNumberCard(int newNumber) { this.number = newNumber; }
    public void setValue (int newValue) { this.value = newValue; }
    So for instance, your above example would look something like this:
    Code:
    Card card1 = new Card();
    card1.setNumberCard(1);
    card1.setValue(11);
    card1.setSuite("hearts");
    Last edited by techieMoe; 08-24-2006 at 07:19 PM.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  8. #8
    Just Joined!
    Join Date
    Aug 2006
    Posts
    59
    Thanks for the help..................... When i put that code in im still getting a bunch of syntax errors..
    Heres the code i have
    Code:
    	public class Card {
    		int numberCard;
    		int value;
    		String suite;
    		
    		
    		public String displaySuite() {
    			return suite;
    		}
    		
    		public int cardNumber() {
    			return numberCard;
    		}
    		
    		public int value() {
    			return value;
    		}
    		
    		public void setSuite (String newSuite) {
    			this.suite = newSuite;
    		}
    		
    		public void setNumberCard (int numCard) {
    			this.numberCard = numCard;
    		}
    		
    		public void setValue (int newValue) {
    			this.value = newValue;
    		}
    		
    	}
    	
    	//Cards
    	
    		Card card1 = new Card();
    		card1.setNumberCard(1);
    		card1.setValue(11);
    		card1.setSuite("hearts");
    it returns these errors:

    -Syntax error on token "1", delete this token
    -Syntax error on token(s), misplaced construct(s)
    -Syntax error on token "11", delete this token
    -Syntax error on token(s), misplaced constructs(s)
    -Syntax error on token ""hearts"", delete this token
    -Syntax error on token(s), misplaced construct(s)

    Im completely lost and frustrated..... thanks AGAIN for the help
    tom

  9. #9
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    The reason is that you're running commands outside of a class. Something like this would work:
    Code:
    public class Card {
        int numberCard;
        int value;
        String suite;
    		
    		
        public String displaySuite() {
            return suite;
        }
    		
        public int cardNumber() {
            return numberCard;
        }
    		
        public int value() {
            return value;
        }
    		
        public void setSuite (String newSuite) {
            this.suite = newSuite;
        }
    		
        public void setNumberCard (int numCard) {
            this.numberCard = numCard;
        }
    		
        public void setValue (int newValue) {
            this.value = newValue;
        }
    
        public static void main(String args[]) {
            Card card1 = new Card();
            card1.setNumberCard(1);
            card1.setValue(11);
            card1.setSuite("hearts");
        }
    }
    See how we put the executing code inside of the main() method? Then, when we run the class, code inside main() gets executed.

    Java is single-paradigm language: EVERYTHING must be inside of a class.

  10. #10
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    You should probably also make the data members private:
    Code:
    public class Card {
    	private int numberCard;
    	private int value;
    	private String suite;
    	
    	
    	public String displaySuite() {
    		return suite;
    	}
    	
    	public int cardNumber() {
    		return numberCard;
    	}
    	
    	public int value() {
    		return value;
    	}
    	
    	public void setSuite (String newSuite) {
    		this.suite = newSuite;
    	}
    	
    	public void setNumberCard (int numCard) {
    		this.numberCard = numCard;
    	}
    	
    	public void setValue (int newValue) {
    		this.value = newValue;
    	}
    	
    }
    If you don't specify the access modifer, methods and members default to package access.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •