2

Okay, so I'll first show my code, then I'll explain what I need help with.

  Random isServerChance = new Random();
  Random isGood = new Random();
  Random isBad = new Random();

  // Some IDs which are considered "Good"
  int isGoodTable[] =
  { 1038, 1040, 1042, 1044 };
  // Some IDs which are considered "Bad"
  int isBadTable[] =
  { 6570, 6585, 952, 969 };
  // Based on this roll, will pick which cat is used. Good, or Bad.
  int isServerRoll = isServerChance.nextInt(6);
  // If it's > 3 it's going to be a "Good" item, if it's not, it's a bad
  // item.
  if (isServerRoll > 3)
  {

     int isGoodValue = isGood.nextInt(isGoodTable.length);
     System.out.println("You've received a rare item! You got a: " + isGoodTable[isGoodValue]);

  } else
  {
     int isBadValue = isBad.nextInt(isBadTable.length);
     System.out.println("You've received a normal item. You've got a: " + isBadTable[isBadValue]);

  }

}

Okay, so this will print: You've received a normal item. You've got a: 969 || You've received a rare item! You got a: 1042.

Let's say the int value of 969 is a Spade. Is it possible for me to make 969 equal to an array of Strings from one of the int tables above?

So, if I land on 969, it'll say: "You've received a normal item. You've got a: Spade!"

I'm sort of new, so I'm not sure if this is even possible, but I don't want to make loads of if statements for each ID.

Thanks

2
  • You can use a Hash(Set, Map), but this won't work with ints, so use Integer class to wrap it. Commented Dec 22, 2013 at 22:35
  • 4
    Use a single Random - no reason to have 3 of them... Commented Dec 22, 2013 at 22:36

5 Answers 5

6

It seems that you simply want arrays of Strings rather than arrays of integers:

String[] isBadTable = { "Foo", "Bar", "Baz", "Spade" };

Read the tutorial on arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't get a Random value from a String though. It'd have to be an int value, then it would need to parse to a String, or is there another way? The way I have it now, it needs an int to run a dice roll. Or is there another way?
I like this answer better than my own, but I'll leave mine as a potentially useful alternative. @user3128007, you're choosing a random index into the array, so this should still work.
Where in the code are you "getting a random value from a string". The length of an array of Strings is still an int. So isBad.nextInt(isBadTable.length) will still work as before.
2

Yes. Use a Map<Integer, String>. You could put your items in like this

Map<Integer, String> itemMap = new HashMap<Integer, String>();
itemMap.put(969, "Spade");
// etc.

Then you can retrieve your values like this

String itemName = itemMap.get(969);  // spade

If you do this, just be careful, because a HashMap doesn't have any default value, so if the key you try to use isn't in the Map you'll get null back.

Comments

2

You can use enums to solve this problem. For example, you could do

public enum CardSuit {
    SPADE(969, "Spades"),
    HEART(952, "Hearts"),
    DIAMOND(6585, "Diamonds"),
    CLUB(6570, "Clubs");

    private final int value;
    private final String name;

    private CardSuit(int value, String name){
        this.value = value;
        this.name = name;
    }

    public int getValue(){
        return value;
    }

    public String toString(){
        return name;
    }

}

Now, you can make an array of CardSuit by using

CardSuit[] suits = CardSuit.class.getEnumConstants();

and you can map the spades to the 969 by calling CardSuit.SPADE.getValue(). Use a Map<Integer, CardSuit> to do a lookup in the other direction.

See The Java Tutorials on Enums for a comprehensive tutorial.

Comments

0

Assuming you need the IDs and Strings (rather than a String table and just use the index as an ID), it looks like you want some sort of HashMap.

import java.util.HashMap;


HashMap<Integer, String[]> IDs = new HashMap<>();
IDs.put(969, new String[]{"Spade", "Shovel"} ); // Setting up your HashMap

// Recalling later
int randomID = 969; 
String[] items = IDs.get(randomID); // items will be "Spade", "Shovel", will be null if the ID is not in the HashMap.

HashMap documentation: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Comments

0

You have the basic idea down. We don't generate random Strings, only random numbrrs. There is no point in declaring more than one Random object. Use one Random generator to create all the numbers you need.

As a beginner, I would not recommend maps to then create a correspondence between the number in the array (item #). You could put the correspondence into a text file. If you will have many items, this is the way to go. Searching through a text file is a good exercise.

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.