0

Trying to create a code that will print out the face value and suit of randomly pulled set of 5 cards. For some reason it only prints out the input to the methods but not the results of the methods. Can someone please tell where I am going wrong? Thanks! for the results I am getting Output:

3 of 1
2 of 5
2 of 6
0 of 11
3 of 12

import java.util.*;

    public class Card {

    static int suits = 0;
    static int values = 0;
    static String c, d,e,f;
     public String getSuit()                
             {
                if (c == "0")
                    e = ("Hearts");             
                else 
                    if (c == "1")
                    e = "Spades";               
                else
                    if (c == "2")
                    e = "Clubs";                    
                else
                    if (c == "3")
                    e = "Diamonds";
                else
                    e=c;

                return e;
                }

            public String getValue() {

                if (d == "0")
                    f = ("Ace");
                else                    
                if (d =="11")
                    f = ("Jack");
                else 
                if (d == "12")
                    f = ("Queen");
                else
                if (d == "13")
                    f = ("King");
                else
                    f =d;
                return f;
            }

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        Random gen = new Random();

        int [] suits = new int[4];
        {
            for (int index=0;index<suits.length; index++)
            suits[index] = index;
        }   


        int [] values = new int[14];
        {
            for (int j = 0; j<values.length;j++)
                values[j]= j;
        }

        for (int g = 0; g<5; g++)
        {
         int a = gen.nextInt(suits.length);

         int b = gen.nextInt(values.length);

         c = Integer.toString(a);

         d = Integer.toString(b);
         //System.out.println(c);
         //System.out.println(d);

         Card draw = new Card ();

         draw.getSuit();
         draw.getValue();

         System.out.println(e + " of "+f);
        }
    }
roshan_nazareth
  • 311
  • 5
  • 16
Joshua
  • 1
  • 1
  • try using `equals` method of `String` class, something like this `if (d.equals("0")){}` – mayank bisht Sep 22 '17 at 05:50
  • Ahhhh static variables in the main class my hearth... No but seriously use parameters to hand some variables over to your method. Try to use static only when you actually get into object oriented programming and know what it does. Saw some of my students doing that and when we went over object oriented programming they had some real problems to figure things out – Alexander Heim Sep 22 '17 at 06:10
  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a more specific question. – Joe C Sep 22 '17 at 06:16

2 Answers2

1
static String c, d,e,f;

all are strings, in Java you have to use .equals to Compare Strings. Hence,

    if (c.equals("0"))
        e = ("Hearts");             
    else 
        if (c.equals"1"))
        e = "Spades";  

and change == to .equals everywhere in the Code. Let us know for more help!

0

You can't use the == operator to compare strings the way you are intending. When using == on an Object, its not the values that are compared but rather the references. That is, does each variable point to the same object in memory.

Instead, you should use the String.equals() method.