1

So I am trying to compare the index of the array of strings to a predetermined value so it can return the type of card it will be based on the rank. I know the below method is comparing the actual string to the number I just have no idea how to compare the actual index Value to the number.

public String findFaceValue()
{
    String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
                              , "Ten", "Jack", "Queen", "King"};
    for(int i = 0; i < faceValueArray.length; ++i)
    {   
        if (faceValueArray[i] == rank)
            return faceValueArray[i];   
    }
    return "Not a valid card";

}
4
  • 1
    What is rank? Commented Mar 1, 2017 at 22:55
  • @Meme Lord What do you mean by compare index value of the card. Do you mean the card's individual value, for instance, Ace is highest in rank? Commented Mar 1, 2017 at 22:55
  • Okay so rank is just any number between 1 and 13 so for example if it's 3 I want the program to return "Three". Commented Mar 1, 2017 at 23:02
  • Okay, nevermind I feel like and idiot I could've just returned faceValueArray[rank] THANKS FOR THE HELP ANYWAY GUYS Commented Mar 1, 2017 at 23:05

3 Answers 3

1

So I am trying to compare the index of the array of strings to a predetermined value so it can return the type of card it will be based on the rank

If you just want to check whether a given index can get you a valid card. If it does, return the card's name (Rank), else return "Not a valid card". This is what you can do:

public String findFaceValue(int index)
{
    String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
                               "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

    if (index < 0 || index >= faceValueArray.length)
        return "Not a valid card";
    return faceValueArray[index];
}

Okay so rank is just any number between 1 and 13 so for example if it's 3 I want the program to return "Three".

To offset the index, just change it to:

if (index < 1 || index > faceValueArray.length)
    return "Not a valid card";
return faceValueArray[index-1];
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly it, but I figured it out on my own. Thanks anyway!
@MemeLord Anyway, I will recommend you to set up a class for Card and write the attributes in Card class instead.
I have card set up as a class, this is just one part of a much bigger project.
0

When comparing Strings in Java you need to use str1.equals(str2). In your case (assuming rank is a String:

public String findFaceValue(String rank)
{
    String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
                              , "Ten", "Jack", "Queen", "King"};
    for(int i = 0; i < faceValueArray.length; ++i)
    {   
        if (faceValueArray[i].equals(rank))
            return faceValueArray[i];   
    }
    return "Not a valid card";

}

If you want to return the card's number value (Ace = 0, Two = 1, ..., Jack = 9, etc.) you could do:

public String findFaceValue(int rank)
{
    String faceValueArray[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
                              , "Ten", "Jack", "Queen", "King"};  
    if(rank < faceValueArray.length && rank >= 0)
        return faceValueArray[rank];

    return "Not a valid card";

}

Comments

0

Try this,

String Name = // insert code here
int index = -1;
for (int i=0;i<arrayName.length;i++) {
if (arrayName[i].equals(Name)) {
    index = i;
    break;
}
}

Also, you can compare with compare to (it compares 2 strings and it returns a number),

   String str1 = "String method tutorial";
   String str2 = "compareTo method example";

   int var1 = str1.compareTo( str2 );
   System.out.println("str1 & str2 comparison: "+var1);

Comments

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.