1

Hi I have been trying to make my first game for a while but have run into difficulty. I want to pull a randomized String from an array of Strings and then print it out but I have errors in my code. I have looked all over but I can't find the solution. Here is the code: (I have already declared the currentRoom varible)

 int currentRoom;
String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
Random rand = new Random()
currentRoom = rooms [rand.nextInt( rooms.length)];
System.out.println(currentRoom);

I have an error on my currentRoom variable in line 6 and I think that is messing everything up, but I don't know so the mistake could be anywhere.

Any help is appreciated:)

asked Apr 15, 2013 at 1:41

3 Answers 3

2

Two things you have to change:

  1. currentRoom should be String Array
  2. Printing string array should be done using Arrays.toString

     String[] currentRoom;
     String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
     Random rand = new Random();
     currentRoom = rooms [rand.nextInt(rooms.length)];
     System.out.println(Arrays.toString(currentRoom));
    

Sample Output

[Goblin Home1, Spider Nest1]

Hope this helps!

answered Apr 15, 2013 at 1:51
Sign up to request clarification or add additional context in comments.

1 Comment

Because later it is going to be more complex
1

Why do you need a 2D array of strings? I think you can manage with a 1D array instead.

String currentRoom;
String[] rooms = {"Start", "Treasure Room1", "Goblin Home1", "Spider Nest1"};
Random rand = new Random();
currentRoom = rooms [rand.nextInt( rooms.length)];
System.out.println(currentRoom);
answered Apr 15, 2013 at 1:56

1 Comment

Because later it is going to get more complex
0
String currentRoom;
String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
Random rand = new Random();
int rnd = rand.nextInt( rooms.length);
currentRoom = rooms [rnd][0] + ":"+ rooms [rnd][1];
System.out.println(currentRoom);
answered Apr 15, 2013 at 1:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.