Search code examples
javaperformancepoker

Why won't my program work?


I have a java program that I had working perfectly, but somehow, overtime it failed. I'm really not sure how and I'm not sure why, so I'm hoping a new pair of eyes will help solve the problem. Please help, thanks! The program is a simple "poker" code that outputs 5 random cards from the deck.

The program was working when I first ran it but for some strange reason it doesn't run anymore.

I keep getting the errors...
"cannot resolve symbol rank.length",
"cannot resolve symbol suit.length",
"Package java.Random does not exist"

The Program:

import java.Random.*;
import java.io.*;
import java.util.*;

public class Poker {

    public static void main(String[] args) {

        //initialize variables
        int suits = suit.length;
        int ranks = rank.length;
        int n = suits * ranks;
        //counter
        int m = 5;

         // create a deck of 52 cards
         String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
         String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                           "Jack", "Queen", "King", "Ace"
                         };


         // initialize deck
         String[] deck = new String[n];
         for (int i = 0; i < ranks; i++) { 
             for (int j = 0; j < suits; j++) { 
                 deck[suits*i + j] = rank[i] + " of " + suit[j]; 

         }
         }

        // create random 5 cards
        for (int i = 0; i < m; i++)  {
            int r = i + (int) (Math.random() * (n-i));
            String t = deck[r];
            deck[r] = deck[i];
            deck[i] = t;
        }

        // print results
        for (int i = 0; i < m; i++){
            System.out.println(deck[i]);

        }        
    }
}

Solution

  • cannot resolve symbol rank.length

    That's because you try to access rank.length before declaring rank.

    Try:

    //first declare arrays
    // create a deck of 52 cards
    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                       "Jack", "Queen", "King", "Ace"
                     };
    
    //then find out their length
    int suits = suit.length;
    int ranks = rank.length;
    int n = suits * ranks;
    //counter
    int m = 5;
    

    Package java.Random does not exist

    It's true, it's import java.util.Random, not import java.Random.*, the java.Random package doesn't exist.