The Artima Developer Community
Sponsored Link

Java Answers Forum
Java question

1 reply on 1 page. Most recent reply: Mar 21, 2002 2:27 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Gordon Hew

Posts: 1
Nickname: saguaro
Registered: Mar, 2002

Java question Posted: Mar 20, 2002 8:42 PM
Reply to this message Reply
Advertisement
I'm trying to enter a string for example: 1h 2s 3h
and store those values in an array for example [0] = (1, h). My code generates a run-time error, can you guys give me some advice. Thanks
public void convertArray(String x)
{
int rank;
char suit, rankChar;
for(int i = 0; i < cards.length(); i++)
{
rank == cards.charAt(i);
rankChar = cards.charAt(i);
suit = cards.charAt(i+1);
if (rankChar == 'a')
rank == Card.charToRank('a');
Card nextCard;
nextCard = new Card(rank,suit);
nextCard = enteredHand;
}
}
*charToRank is a switch statement that returns a number


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Java question Posted: Mar 21, 2002 2:27 PM
Reply to this message Reply


/** Takes a String in the format 1h 2s 3h 10s jc qd, processes it into a vector of Card objects.
*/
public Vector sortString(String s){
Vector v = new Vector();
//assume string is in format 1h 2s 3h 10s jc qd
StringTokenizer st = new StringTokenizer(s, " "); //assume space is separator
while (st.hasMoreTokens){
String nextcardstring = st.nextToken();
if (checkData(nextcardstring)) {
Card nextcard = new Card(nextcardstring)
v.add(nextcard);
}
}

}

private boolean checkData(String s){
boolean checkfirst = false;
boolean checksecond = false;
char c0 = charAt(0);
char c1 = charAt(1);
if (c0 == 'h') checkfirst = true; //hearts
if (c0 == 'd') checkfirst = true; //diamonds
if (c0 == 's') checkfirst = true; //spades
if (c0 == 'c') checkfirst = true; //clubs

if (c1 == 'a') checksecond = true; //ace
if (c1 == '2') checksecond = true; //2
if (c1 == '3') checksecond = true; //3
if (c1 == '4') checksecond = true; //4
if (c1 == '5') checksecond = true; //5
if (c1 == '6') checksecond = true; //6
if (c1 == '7') checksecond = true; //7
if (c1 == '8') checksecond = true; //8
if (c1 == '9') checksecond = true; //9
if (c1 == '1') {
char c2 = charAt(2);
if (c2 == '0') {
checksecond = true; //10
}
}
if (c1 == 'j') checksecond = true; //jack
if (c1 == 'q') checksecond = true; //queen
if (c1 == 'k') checksecond = true; //king

return checkfirst && checksecond;
}

/** Inner class to hold the card data
*/
class Card{

protected String rank;
protected String suit;

public Card(String s){
rank = s.substring(0,1);
suit = s.substring(1);
}

public String getRank(){
return rank;
}

public String getSuit(){
return rank;
}

public void setRank(String rank){
this.rank = rank;
}

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

}

Flat View: This topic has 1 reply on 1 page
Topic: ExcelAccessor Bean Suite Previous Topic   Next Topic Topic: voice chatting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use