-3

I am trying to create a list of random colors using a random generator, which will be used to create a game where the user memorizes the colors. The issue is that my list of colors keeps printing with brackets around it. Any help finding a solution to this is much appreciated!

import java.util.Random;
import javax.swing.JOptionPane;
import java.util.Arrays;

public class Mission13Rivera
{

   public static void main (String[] args)
   {
      //Opening Dialog
      MemoryGame mG = new MemoryGame ();

      Random r = new Random();

      String[] colors = {"red", "white", "yellow", "green", "blue", "orange", "purple", "black", "pink", "gray"};
      String[] solution = new String[6];

      for (int i = 0; i < solution.length; i++)
      {
         solution[i] = colors[r.nextInt(10)];
      }

      JOptionPane.showMessageDialog(null,"The colors are: " + `Arrays.toString(solution));`

   }

}

The result should look like... "The colors are: green, yellow, purple, pink, blue" or whatever other random number combination.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
emmiree
  • 1
  • 2
  • 4
    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately. – CertainPerformance Jun 21 '19 at 01:58
  • @CertainPerformance you made my day!! :D xd. – MD Ruhul Amin Jun 21 '19 at 01:59
  • 2
    Just started coding, so definitely still learning all of the terms. I appreciate you letting me know, will be learning the differences now. Thanks! – emmiree Jun 21 '19 at 02:02
  • Possible duplicate of [Output ArrayList to String without \[,\] (brackets) appearing](https://stackoverflow.com/questions/32774059/output-arraylist-to-string-without-brackets-appearing) – ordonezalex Jun 21 '19 at 02:52
  • @emmiree Welcome to stackoverflow! Hopefully we don't scare you away from coding, because it's lots of fun. Try looking at this question, because I think you will find your answer: https://stackoverflow.com/q/32774059 – ordonezalex Jun 21 '19 at 02:54
  • @ordonezalex Thanks for linking that! Very helpful. – emmiree Jun 21 '19 at 03:19

2 Answers2

0

The only way to do that would be looping over the solution array and concatenating the values into a string to be displayed in your JOptionPane. The Arrays.toString() methods will not give the output you want.

Henrique Sabino
  • 546
  • 3
  • 19
0

I don't write much Java, but you're gonna need to loop through the array and construct a string of the values.

String result = "";
for(String color in solution) {
    result += " " + color;
}

A better solution, found here

Arrays.toString(solution).replace("[", "").replace("]", "");
Ace
  • 1,028
  • 2
  • 10
  • 22