I'm trying to create a boggle game. I have it so it will setup the board and accept user input. What I am trying to do now is get rid of the "[" braces "]" and the commas to give it a cleaner look and to hopefully make it easier when i try and run a solution check.

Can someone please assist me in getting rid of the braces and commas? I appreciate it.

package testrun3;

import java.util.*;

public class testRun3
{
    static String[] alpha = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Qu","R","S","T","U","V","W","X","Y","Z"};
    static Random rand = new Random();

    static ArrayList<String> row1 = new ArrayList<String>();
    static ArrayList<String> row2 = new ArrayList<String>();
    static ArrayList<String> row3 = new ArrayList<String>();
    static ArrayList<String> row4 = new ArrayList<String>();
    static ArrayList<String> row5 = new ArrayList<String>();

    public static void main(String[] args)
    {
        code();

        System.out.println(row1);
        System.out.println(row2);
        System.out.println(row3);
        System.out.println(row4);
        System.out.println(row5);

        System.out.println("To end game enter '//'");
        System.out.println("Enter words found: ");
        endGame();
    }

    public static void code()
    {
        row1.add(alpha[produceNumber()]);
        row1.add(alpha[produceNumber()]);
        row1.add(alpha[produceNumber()]);
        row1.add(alpha[produceNumber()]);
        row1.add(alpha[produceNumber()]);

        row2.add(alpha[produceNumber()]);
        row2.add(alpha[produceNumber()]);
        row2.add(alpha[produceNumber()]);
        row2.add(alpha[produceNumber()]);
        row2.add(alpha[produceNumber()]);

        row3.add(alpha[produceNumber()]);
        row3.add(alpha[produceNumber()]);
        row3.add(alpha[produceNumber()]);
        row3.add(alpha[produceNumber()]);
        row3.add(alpha[produceNumber()]);

        row4.add(alpha[produceNumber()]);
        row4.add(alpha[produceNumber()]);
        row4.add(alpha[produceNumber()]);
        row4.add(alpha[produceNumber()]);
        row4.add(alpha[produceNumber()]);

        row5.add(alpha[produceNumber()]);
        row5.add(alpha[produceNumber()]);
        row5.add(alpha[produceNumber()]);
        row5.add(alpha[produceNumber()]);
        row5.add(alpha[produceNumber()]);
    }
    
    public static int produceNumber()
    {
        return (rand.nextInt(alpha.length));
    }

    public static void endGame()
    {
        Scanner userInput = new Scanner(System.in);
        String input;
        input = userInput.nextLine().toUpperCase();
        if(input.contains("//"))
        {
            System.exit(0);
        }
        else
        {
            endGame();
        }

    }// close main
}

Recommended Answers

All 2 Replies

Use a for loop, to loop through the ArrayList and print its elements instead of the System.out.println(row1)

The commas and the brackets are a part of the ArrayList class's toString() implementation. If you need your own row representation, either write your own method which does so or better yet, create a custom class called BoggleRow which uses composition or inheritance to mimic the ArrayList class. Something like:

public class TestRun3 {
    // your code here

    public void displayRow(List row) {
        // code which loops over the list & displays the row
    }
}

//OR

public class BoggleRow {
    private List row;
    public BoggleRow() {
        row = new ArrayList();
    }

    @Override
    public String toString() {
        // your code here
    }
}

// OR

public class BoggleRow extends ArrayList {
    
    public BoggleRow() {
        super();
    }

    @Override
    public String toString() {
        // your code here
    }
}

The above code gives you a rough sketch of how your class would look like. My personal preference would be the second implementation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.