0

I am new to java and I am having trouble with this.I need to make 2 different programs. one to make txt file and another to execute. First program makes grid. 2nd reads txt in method and then I am supposed to call it in the main. I can't seem to figure it out.

Initial array (txt.file):

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.Collections;

public class Grid {
    public static void main(String[] args) {
        ArrayList<Integer> indNum = new ArrayList<Integer>();
        ArrayList<Integer> elemNum = (ArrayList<Integer>) indNum.clone();
        ranNum(indNum);
        ranNum(elemNum);

        String grid[][] = mGrid();
        replaceGrid(grid, indNum.get(0), elemNum.get(0));
        replaceGrid(grid, indNum.get(1), elemNum.get(1));
        replaceGrid(grid, indNum.get(2), elemNum.get(2));
        disGrid(grid);
    }

    public static String[][] mGrid() {
        String[][] Array = { { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" },
                             { " *", " *", " *", " *", " *" }       
                           };
        return Array;
    }

    public static void disGrid(String[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
    }

    public static ArrayList<Integer> ranNum(ArrayList<Integer> ran) {
        for (int i = 0; i < 5; i++) {
            ran.add((i));
        }
        Collections.shuffle(ran);
        ran.remove(4);
        ran.remove(3);
        return ran;
    }

    public static String[][] replaceGrid(String[][] array, int num1, int num2) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                array[num1][num2] = " O";
            }
        }
        return array;
    }
}

Output:

 O * * * *
 * * * * *
 * * * * *
 * * O * *
  * * * O *

three O's are placed randomly in the grid 2nd Part Read in and display by executing the array:

package Execute;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.Collections;
import java.io.*;

  public class part2 {
    public static void main(String[] args) {
        ArrayList<String> makeGrid= new ArrayList<String>();
         readFile(makeGrid);            
        System.out.print(makeGrid); 
    }

    public static void readFile(ArrayList<String> exec) {
        try {
            Scanner reader = new Scanner(new File("Grid.txt"));
            do {
                exec.add(reader.nextLine());
            }while (reader.hasNext());
        } catch (FileNotFoundException fnf) {
            System.out.println("File was not found");
        }
    }   
}

When I run this it does not store the old grid and display it. Instead it just prints out the whole code I wrote in one line. How do i execute the code and store it in arraylist so i can manipulate it?

3
  • 1
    Welcome to SO! Your question is very unclear, unfortunately. It is not clear what the program is meant to do or what your goal is. Please review the How to Ask article and consider explaining precisely what you expect and what you are getting instead. Commented Sep 22, 2018 at 5:31
  • Got it. thank you ! i will delete it and repost it more clear. Commented Sep 22, 2018 at 5:35
  • Repost by undeleting after editing, please. Commented Sep 22, 2018 at 5:38

1 Answer 1

1

First, you would want while (reader.hasNextLine())

You can manually add the newline back if you want the arraylist to be displayed as multiple lines

exec.add(reader.nextLine() + "\n");

Or you can actually print every line rather than only the arraylist

readFile(makeGrid);
for (String line : makeGrid) {
    System.out.println(line);
}

By the way, it's not recommended to pass in mutable collections into methods as parameters. You should return a new arraylist from readFile()

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

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.